mimiqcircuits.decomposition.basis.canonical¶
Canonical decomposition basis targeting GateU and GateCX.
Classes
|
Decomposition basis targeting |
- class CanonicalBasis(borrow=False, max_ancillas=0, allow_measurement=False)[source]¶
Bases:
DecompositionBasisDecomposition basis targeting
GateUandGateCX.Every gate is rewritten into
GateU(an arbitrary single-qubit gate) andGateCX, with the built-in decomposition of each operation (CanonicalRewrite). The non-unitary primitives are kept as they are;isterminal()lists them. This is the default basis ofdecompose().- 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(GateH(), 0) 1-qubit circuit with 1 instruction: └── H @ q[0] >>> c.push(GateSWAP(), 0, 1) 2-qubit circuit with 2 instructions: ├── H @ q[0] └── SWAP @ q[0:1] >>> c.push(Measure(), 0, 0) 2-qubit, 1-bit circuit with 3 instructions: ├── H @ q[0] ├── SWAP @ q[0:1] └── M @ q[0], c[0] >>> # gates become GateU and GateCX; the measurement is kept >>> decompose(c, CanonicalBasis()) 2-qubit, 1-bit circuit with 5 instructions: ├── U((1/2)*pi, 0, pi, 0.0) @ q[0] ├── CX @ q[0], q[1] ├── CX @ q[1], q[0] ├── CX @ q[0], q[1] └── M @ q[0], c[0]
- isterminal(op)[source]¶
Check whether an operation is terminal in the canonical basis.
The terminal operations are
GateUandGateCX;Measure;Reset,ResetXandResetY;Loss,Reload,CheckandMeasureCheck;BarrierandDelay; classical operations and annotations; noise channels andReadoutErr;ExpectationValue,Amplitude,SchmidtRank,VonNeumannEntropyandBondDim; and the z-register operationsAdd,MultiplyandPow. AnIfStatementorWhileStatementis terminal when its body is.
- decompose(op, qubits, bits, zvars)[source]¶
Rewrite a non-terminal operation with its built-in decomposition.
- Parameters:
- Returns:
A new
Circuitwith one decomposition step.- Raises:
DecompositionError – If
ophas 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.