mimiqcircuits.decomposition.decompose¶
- decompose(source, basis=None, wrap=False)[source]¶
Recursively decompose a circuit to a target basis.
Rewrites every operation of
sourcethat is not terminal inbasis, 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
RewriteRuleis wrapped in aRuleBasis, so it is applied until it no longer matches. Defaults toCanonicalBasis.wrap (bool) – If
False(default), the result holds only terminal operations. IfTrue, each non-terminal gate becomes aGateCallto aGateDeclholding its decomposition, and each other non-terminal operation (such asMeasureReset) aBlockholding its decomposition, both wrapped again at every level. Gates that differ only in their parameters share oneGateDecl. A gate whose decomposition measures an ancilla (allow_measurement=True) becomes aBlockCallto a sharedBlockDecl. This keeps the structure of the original circuit visible.
- Returns:
A new
Circuitof terminal operations, plusGateCallandBlockoperations whenwrap=True.- Raises:
TypeError – If
sourceis not a circuit, instruction or operation.DecompositionError – If
basiscannot decompose an operation, for example aGateTinStimBasis.
- Return type:
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]