mimiqcircuits.decomposition.basis.qasm

OpenQASM 2.0 decomposition basis.

Classes

QASMBasis([borrow, max_ancillas, ...])

Decomposition basis targeting the OpenQASM 2.0 gate library.

class QASMBasis(borrow=False, max_ancillas=0, allow_measurement=False)[source]

Bases: DecompositionBasis

Decomposition basis targeting the OpenQASM 2.0 gate library.

The terminal gates are the OpenQASM 2.0 built-ins and the gates of the qelib1.inc include file that have a direct MIMIQ counterpart, so a decomposed circuit can be written as OpenQASM 2.0 and read back by any parser that ships the standard qelib1.inc.

The terminal operations are:

  • the OpenQASM 2.0 built-ins GateU and GateCX;

  • the single-qubit gates of qelib1.inc: GateU3, GateU2, GateU1, GateID, GateX, GateY, GateZ, GateH, GateS, GateSDG, GateT, GateTDG, GateRX, GateRY and GateRZ;

  • the two-qubit gates of qelib1.inc: GateCY, GateCZ, GateCH, GateCRX, GateCRY, GateCRZ, GateSWAP, GateRXX and GateRZZ;

  • the three- and four-qubit gates of qelib1.inc: GateCCX, GateCSWAP and GateC3X;

  • Measure, Reset and Barrier, but not the X and Y variants of Measure and Reset;

  • annotations, such as AncillaAlloc, which the OpenQASM writer drops;

  • an IfStatement whose body is terminal;

  • a GateCall whose declaration holds only terminal operations.

Every other operation is decomposed through CanonicalRewrite. Operations that OpenQASM 2.0 cannot express and that have no decomposition into the gates above (noise channels, classical operations, Z-register operations, …) raise DecompositionError.

MeasureReset decomposes into Measure followed by Reset. This leaves the same state and bit as its canonical decomposition, a measurement and an X conditioned on its outcome, and unlike that conditional it can be written for a circuit of any number of bits. OpenQASM 2.0 has no loops, so WhileStatement raises DecompositionError.

Parameters:
  • borrow (bool) – Let decompositions borrow qubits of the circuit that the operation does not act on. A borrowed qubit can be in any state; the decomposition leaves it as it found it.

  • max_ancillas (int) – Let decompositions add up to this many clean ancillas, numbered after the qubits of the circuit and marked with AncillaAlloc and AncillaFree. Ancillas freed by one operation are reused by the next.

  • allow_measurement (bool) – With max_ancillas, let decompositions undo steps by measuring an ancilla into one extra bit, numbered after the bits of the circuit. A circuit without bits is refused with a ValueError.

The three ancilla options are off by default, and only change how multi-controlled X gates are decomposed. Circuits whose width is unknown, such as a lone operation, get no ancillas.

Examples

>>> from mimiqcircuits import *
>>> c = Circuit()
>>> c.push(GateCCX(), 0, 1, 2)
3-qubit circuit with 1 instruction:
└── C₂X @ q[0:1], q[2]

>>> c.push(GateSX(), 0)
3-qubit circuit with 2 instructions:
├── C₂X @ q[0:1], q[2]
└── SX @ q[0]

>>> c.push(MeasureReset(), 0, 0)
3-qubit, 1-bit circuit with 3 instructions:
├── C₂X @ q[0:1], q[2]
├── SX @ q[0]
└── MR @ q[0], c[0]

>>> # CCX is in qelib1.inc and is kept; SX is not and is rewritten;
>>> # MeasureReset becomes a measurement and a reset
>>> decompose(c, QASMBasis())
3-qubit, 1-bit circuit with 7 instructions:
├── C₂X @ q[0:1], q[2]
├── S† @ q[0]
├── H @ q[0]
├── S† @ q[0]
├── U(0, 0, 0, (1/4)*pi) @ q[0]
├── M @ q[0], c[0]
└── Reset @ q[0]

See also

CanonicalBasis, CliffordTBasis, StimBasis

isterminal(op)[source]

Check whether an operation is terminal in the OpenQASM 2.0 basis.

Parameters:

op (Operation) – The operation to check.

Returns:

True if op is one of the terminal operations listed in the class description.

Return type:

bool

decompose(op, qubits, bits, zvars)[source]

Rewrite a non-terminal operation one step toward OpenQASM 2.0.

MeasureReset becomes Measure and Reset; every other operation goes through CanonicalRewrite.

Parameters:
  • op (Operation) – The operation to rewrite.

  • qubits (Sequence[int]) – Qubit indices the operation acts on.

  • bits (Sequence[int]) – Classical bit indices the operation acts on.

  • zvars (Sequence[int]) – Z-variable indices the operation acts on.

Returns:

A new Circuit with the rewrite.

Raises:

DecompositionError – If op is a WhileStatement or has no built-in decomposition.

Return type:

Circuit

decompose_ctx(op, qubits, bits, zvars, ctx)[source]

Decompose with a context, which only some operations use.

Operations without a rule that uses the context go to decompose(), so an override of it keeps being called.