mimiqcircuits.decomposition.decompose

decompose(source, basis=None, wrap=False)[source]

Recursively decompose a circuit to a target basis.

Rewrites every operation of source that is not terminal in basis, and repeats on the result, until only terminal operations are left. The source is not modified.

Parameters:
  • source (Circuit | Instruction | Operation) – The circuit, instruction or operation to decompose. A bare operation is placed on qubits, bits and z-variables 0, 1, ....

  • basis (DecompositionBasis | RewriteRule | None) – The target basis. A RewriteRule is wrapped in a RuleBasis, so it is applied until it no longer matches. Defaults to CanonicalBasis.

  • wrap (bool) – If False (default), the result holds only terminal operations. If True, each non-terminal gate becomes a GateCall to a GateDecl holding its decomposition, and each other non-terminal operation (such as MeasureReset) a Block holding its decomposition, both wrapped again at every level. Gates that differ only in their parameters share one GateDecl. A gate whose decomposition measures an ancilla (allow_measurement=True) becomes a BlockCall to a shared BlockDecl. This keeps the structure of the original circuit visible.

Returns:

A new Circuit of terminal operations, plus GateCall and Block operations when wrap=True.

Raises:
Return type:

Circuit

Examples

>>> from mimiqcircuits import *
>>> c = Circuit()
>>> c.push(GateH(), 0)
1-qubit circuit with 1 instruction:
└── H @ q[0]

>>> c.push(GateCCX(), 0, 1, 2)
3-qubit circuit with 2 instructions:
├── H @ q[0]
└── C₂X @ q[0:1], q[2]
>>> # the default basis leaves only GateU and GateCX
>>> sorted({type(inst.operation).__name__ for inst in decompose(c)})
['GateCX', 'GateU']
>>> # any other basis can be given
>>> sorted({type(inst.operation).__name__ for inst in decompose(c, CliffordTBasis())})
['GateCX', 'GateH', 'GateT', 'GateTDG']
>>> # with wrap=True each gate becomes a call to a declared gate,
>>> # so the two CH gates share one declaration
>>> c = Circuit()
>>> c.push(GateCH(), 0, 1)
2-qubit circuit with 1 instruction:
└── CH @ q[0], q[1]

>>> c.push(GateCH(), 1, 2)
3-qubit circuit with 2 instructions:
├── CH @ q[0], q[1]
└── CH @ q[1], q[2]

>>> decompose(c, wrap=True)
3-qubit circuit with 2 instructions:
├── mimiq_CH() @ q[0:1]
└── mimiq_CH() @ q[1:2]