mimiqcircuits.decomposition.rules.canonical

Canonical rewrite rule for decomposing operations to GateU and GateCX.

Classes

CanonicalRewrite()

Rewrite rule that applies the built-in decomposition of each operation.

class CanonicalRewrite[source]

Bases: RewriteRule

Rewrite rule that applies the built-in decomposition of each operation.

Every operation class defines one decomposition step in terms of simpler operations; this rule applies it. Repeated, the steps end at GateU, GateCX and the non-unitary primitives, which is what CanonicalBasis does. One step can stop earlier: GateCCX, for example, becomes GateH, GateT, GateTDG and GateCX.

It is the default rule of decompose_step() and the fallback of the other bases.

Examples

>>> from mimiqcircuits import *
>>> rule = CanonicalRewrite()
>>> # H has a built-in decomposition into GateU
>>> rule.matches(GateH())
True
>>> rule.decompose_step(GateH(), [0], [], [])
1-qubit circuit with 1 instruction:
└── U((1/2)*pi, 0, pi, 0.0) @ q[0]

>>> # GateU is a primitive, so there is nothing to rewrite
>>> rule.matches(GateU(0.1, 0.2, 0.3))
False
matches(op)[source]

Check whether an operation has a built-in decomposition.

Matches every operation whose supports_canonical_rewrite() is true. The answer does not depend on any basis: an operation that CanonicalBasis keeps terminal (such as ResetX) still matches, so other bases can expand it. Primitives without a decomposition (GateU, GateCX, Measure, Reset, Barrier, …) do not match.

Parameters:

op (Operation) – The operation to check.

Returns:

True if op has a built-in decomposition.

Return type:

bool

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

Apply one step of the built-in decomposition of an operation.

No check is made that matches() accepts op. A primitive returns a circuit holding just itself.

Parameters:
  • op (Operation) – The operation to decompose.

  • 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 the decomposition.

Return type:

Circuit

decompose_step_ctx(op, qubits, bits, zvars, ctx)[source]

Apply this rule with a DecompositionContext.

Rules that use the context, for example to borrow qubits, override this method. The default drops the context and calls decompose_step().