mimiqcircuits.decomposition.basis.qasm¶
OpenQASM 2.0 decomposition basis.
Classes
|
Decomposition basis targeting the OpenQASM 2.0 gate library. |
- class QASMBasis(borrow=False, max_ancillas=0, allow_measurement=False)[source]¶
Bases:
DecompositionBasisDecomposition basis targeting the OpenQASM 2.0 gate library.
The terminal gates are the OpenQASM 2.0 built-ins and the gates of the
qelib1.incinclude 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 standardqelib1.inc.The terminal operations are:
the OpenQASM 2.0 built-ins
GateUandGateCX;the single-qubit gates of
qelib1.inc:GateU3,GateU2,GateU1,GateID,GateX,GateY,GateZ,GateH,GateS,GateSDG,GateT,GateTDG,GateRX,GateRYandGateRZ;the two-qubit gates of
qelib1.inc:GateCY,GateCZ,GateCH,GateCRX,GateCRY,GateCRZ,GateSWAP,GateRXXandGateRZZ;the three- and four-qubit gates of
qelib1.inc:GateCCX,GateCSWAPandGateC3X;Measure,ResetandBarrier, but not the X and Y variants ofMeasureandReset;annotations, such as
AncillaAlloc, which the OpenQASM writer drops;an
IfStatementwhose body is terminal;a
GateCallwhose 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, …) raiseDecompositionError.MeasureResetdecomposes intoMeasurefollowed byReset. This leaves the same state and bit as its canonical decomposition, a measurement and anXconditioned 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, soWhileStatementraisesDecompositionError.- 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
AncillaAllocandAncillaFree. 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 aValueError.
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- decompose(op, qubits, bits, zvars)[source]¶
Rewrite a non-terminal operation one step toward OpenQASM 2.0.
MeasureResetbecomesMeasureandReset; every other operation goes throughCanonicalRewrite.- Parameters:
- Returns:
A new
Circuitwith the rewrite.- Raises:
DecompositionError – If
opis aWhileStatementor has no built-in decomposition.- Return type:
- 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.