mimiqcircuits.decomposition.basis.canonical

Canonical decomposition basis targeting GateU and GateCX.

Classes

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

Decomposition basis targeting GateU and GateCX.

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

Bases: DecompositionBasis

Decomposition basis targeting GateU and GateCX.

Every gate is rewritten into GateU (an arbitrary single-qubit gate) and GateCX, 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 of decompose().

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(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 GateU and GateCX; Measure; Reset, ResetX and ResetY; Loss, Reload, Check and MeasureCheck; Barrier and Delay; classical operations and annotations; noise channels and ReadoutErr; ExpectationValue, Amplitude, SchmidtRank, VonNeumannEntropy and BondDim; and the z-register operations Add, Multiply and Pow. An IfStatement or WhileStatement is terminal when its body is.

Parameters:

op (Operation) – The operation to check.

Returns:

True if op is terminal.

Return type:

bool

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

Rewrite a non-terminal operation with its built-in decomposition.

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 one decomposition step.

Raises:

DecompositionError – If op 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.