mimiqcircuits.decomposition.rules¶
Rewrite rules for quantum circuit decomposition.
- Available rules:
CanonicalRewrite: Uses built-in gate decomposition methodsFlattenContainers: Flattens GateCall/Block containersZYZRewrite: ZYZ Euler angle decompositionSpecialAngleRewrite: Decompose rotations with special angles to Clifford+TToZRotationRewrite: Convert RX/RY to RZ + Clifford gatesToffoliToCliffordTRewrite: Optimal Toffoli -> Clifford+T rewriteSolovayKitaevRewrite: Solovay-Kitaev approximation with Clifford+TGridSynthRewrite: Ross-Selinger approximation of RZ with Clifford+T
- class mimiqcircuits.decomposition.rules.CanonicalRewrite[source]¶
Bases:
RewriteRuleRewrite rule that decomposes operations using their built-in decomposition.
This rule delegates to each operation’s
_decomposemethod, which typically reduces gates to a minimal set of primitives (GateU, GateCX, etc.).The canonical decomposition is the default decomposition defined by each gate class. Most gates decompose down to GateU (arbitrary single-qubit) and GateCX (two-qubit entangling gate).
Example
>>> from mimiqcircuits import GateH >>> from mimiqcircuits.decomposition import CanonicalRewrite >>> rule = CanonicalRewrite() >>> rule.matches(GateH()) True >>> circ = rule.decompose_step(GateH(), [0], [], []) >>> print(circ) 1-qubit circuit with 1 instruction: └── U((1/2)*pi, 0, pi, 0.0) @ q[0]
- matches(op)[source]¶
Check if this rule can decompose the operation.
Matches every operation that has a canonical decomposition, that is whose
supports_canonical_rewrite()is true. The answer does not depend on any basis: an operation thatCanonicalBasiskeeps terminal (such asResetX) still matches, so other bases can expand it. Primitives without a decomposition (GateU,GateCX,Measure,Reset,Barrier, …) do not match.
- class mimiqcircuits.decomposition.rules.FlattenContainers[source]¶
Bases:
RewriteRuleFlatten container operations into their constituent instructions.
This rule expands: -
GateCall-Block-Inverse(GateCall)-Inverse(Block)(if present)The rule does not perform gate-level decomposition; it only expands container structure. Decomposition behavior is delegated to
CanonicalRewrite.
- class mimiqcircuits.decomposition.rules.ZYZRewrite[source]¶
Bases:
RewriteRuleRewrite rule for ZYZ Euler angle decomposition.
Decomposes single-qubit unitaries into the ZYZ Euler angle form:
RZ(phi) * RY(theta) * RZ(lambda)This is a standard decomposition for arbitrary single-qubit gates, producing only Z and Y rotations (plus a global phase for GateU).
- Supported operations:
GateU(theta, phi, lambda, gamma)->RZ(lambda) * RY(theta) * RZ(phi) * Phase(gamma)GateRX(theta)->S * RY(theta) * Sdg
Identity rotations (angle = 0) are omitted from the output.
Example
>>> from mimiqcircuits import GateU >>> from mimiqcircuits.decomposition import ZYZRewrite >>> from symengine import pi >>> rule = ZYZRewrite() >>> circ = rule.decompose_step(GateU(pi/2, pi/4, pi/3), [0], [], [])
- matches(op)[source]¶
Check if this rule can decompose the operation.
Matches GateU (unless it’s identity) and GateRX.
- decompose_step(op, qubits, bits, zvars)[source]¶
Apply ZYZ decomposition to the operation.
- Parameters:
- Returns:
A Circuit containing the decomposed instructions.
- Raises:
DecompositionError – If the operation is not supported.
- Return type:
- class mimiqcircuits.decomposition.rules.SpecialAngleRewrite(only_cliffords=False)[source]¶
Bases:
RewriteRuleRewrite rule for decomposing rotations with special angles to Clifford+T.
Decomposes single-qubit rotations whose angle is a multiple of pi/4 into explicit Clifford or Clifford+T gates.
This rule only matches rotations with angles k*pi/4 (or k*pi/2 in Clifford-only mode). Generic rotations with arbitrary angles are not matched.
- Parameters:
only_cliffords (bool) – If True, only match angles k*pi/2 (Clifford gates only). If False (default), match angles k*pi/4 (Clifford+T).
- Supported operations:
GateRX(theta)where theta = k*pi/4GateRY(theta)where theta = k*pi/4GateRZ(lambda)where lambda = k*pi/4
Example
>>> from mimiqcircuits import GateRZ >>> from mimiqcircuits.decomposition import SpecialAngleRewrite >>> from symengine import pi >>> rule = SpecialAngleRewrite() >>> rule.matches(GateRZ(pi/4)) # True, decomposes to T True >>> rule.matches(GateRZ(0.123)) # False, not a special angle False
- decompose_step(op, qubits, bits, zvars)[source]¶
Decompose the rotation to Clifford+T gates.
- Parameters:
- Returns:
A Circuit containing the decomposed instructions.
- Raises:
DecompositionError – If the operation cannot be decomposed.
- Return type:
- __init__(only_cliffords=False)¶
- class mimiqcircuits.decomposition.rules.ToZRotationRewrite[source]¶
Bases:
RewriteRuleRewrite rule that converts RX and RY rotations to RZ + Clifford gates.
This is useful for gate sets where only Z-rotations are available as the parametric rotation (e.g., some hardware native gate sets).
- Transformations:
RX(theta)->H * RZ(theta) * HRY(theta)->Sdg * H * RZ(theta) * H * S
Example
>>> from mimiqcircuits import GateRX, GateRY >>> from mimiqcircuits.decomposition import ToZRotationRewrite >>> from symengine import pi >>> rule = ToZRotationRewrite() >>> circ = rule.decompose_step(GateRX(pi/3), [0], [], [])
- decompose_step(op, qubits, bits, zvars)[source]¶
Convert RX or RY to RZ + Clifford gates.
- Parameters:
- Returns:
A Circuit containing the decomposed instructions.
- Raises:
DecompositionError – If the operation is not supported.
- Return type:
- class mimiqcircuits.decomposition.rules.ToffoliToCliffordTRewrite[source]¶
Bases:
RewriteRuleDecompose
GateCCXinto an explicit Clifford+T sequence.
- class mimiqcircuits.decomposition.rules.SolovayKitaevRewrite(depth=3, simplify=True, net_max_depth=10, net_max_points=50000, net_min_dist=0.02)[source]¶
Bases:
RewriteRuleRewrite rule using the Solovay-Kitaev algorithm.
Approximates arbitrary single-qubit unitaries using a sequence of Clifford+T gates. The algorithm recursively refines an initial approximation using group commutators, achieving ε-approximation with O(log^c(1/ε)) gates.
- Parameters:
depth (int) – Recursion depth (default: 3). Higher = better precision, more gates.
simplify (bool) – Whether to simplify the resulting sequence (default: True).
net_max_depth (int) – Maximum depth for epsilon-net generation (default: 15).
net_max_points (int) – Maximum points in epsilon-net (default: 100,000).
net_min_dist (float) – Minimum distance between epsilon-net points (default: 0.01).
- Supported operations:
GateRZ(λ): Z-rotation by angle λ
GateRY(θ): Y-rotation by angle θ
GateRX(θ): X-rotation by angle θ
GateU(θ, φ, λ, γ): General single-qubit unitary
Symbolic parameters are NOT supported.
Example
>>> from mimiqcircuits import GateRZ >>> from mimiqcircuits.decomposition import SolovayKitaevRewrite >>> rule = SolovayKitaevRewrite(depth=0) >>> circ = rule.decompose_step(GateRZ(0.123), [0], [], [])
Note
The first call may be slow due to epsilon-net generation. Subsequent calls reuse the cached net.
- matches(op)[source]¶
Check if this rule can decompose the operation.
Matches single-qubit rotations with concrete (non-symbolic) angles.
- decompose_step(op, qubits, bits, zvars)[source]¶
Decompose the operation using Solovay-Kitaev.
- Parameters:
- Returns:
A Circuit containing the Clifford+T approximation.
- Raises:
DecompositionError – If the operation cannot be decomposed.
- Return type:
- __init__(depth=3, simplify=True, net_max_depth=10, net_max_points=50000, net_min_dist=0.02)¶
- class mimiqcircuits.decomposition.rules.GridSynthRewrite(epsilon=1e-10, seed=0)[source]¶
Bases:
RewriteRuleRewrite rule that approximates
GateRZwith a Clifford+T sequence, using the Ross–Selinger gridsynth algorithm.For
GateRZ(λ)the rule emits a sequence ofGateH,GateS,GateSDG,GateZ,GateTandGateXwhose unitaryVsatisfiesmin_φ ‖RZ(λ) − e^{iφ}V‖ ≤ epsilonin operator norm. The approximation holds up to a global phase, which is dropped. The T-count is about3·log₂(1/epsilon)and, except for rare failures of the integer factoring step, optimal among ancilla-free Clifford+T circuits with this error. Angles that are multiples ofπ/4give their exact short decomposition.The factoring step is randomized. For a fixed
seed, the same angle and tolerance always give the same sequence. Results are cached per angle, since circuits often repeat the same rotation.The synthesis runs in the compiled
mimiqcircuits._coreextension without holding the GIL, so several threads can synthesize in parallel.- Parameters:
- Raises:
ValueError – If
epsilonorseedis out of range.TypeError – If
epsilonis not a real number orseedis not an integer.
- Supported operations:
GateRZ(λ)with a numeric angleλ.
Symbolic angles are not matched.
GateRX,GateRYandGateUmust first be reduced to Z-rotations, for example withZYZRewriteandToZRotationRewrite.CliffordTBasisdoes this and uses this rule by default.Example
>>> from mimiqcircuits import GateRZ, GateT >>> from mimiqcircuits.decomposition import GridSynthRewrite >>> def tcount(c): ... return sum(isinstance(i.operation, GateT) for i in c) >>> # Approximate one rotation to the default tolerance 1e-10 >>> circ = GridSynthRewrite().decompose_step(GateRZ(0.123), [0], [], []) >>> # A coarser tolerance gives fewer T gates >>> coarse = GridSynthRewrite(epsilon=1e-4).decompose_step(GateRZ(0.123), [0], [], []) >>> tcount(coarse) < tcount(circ) True
References
N. J. Ross and P. Selinger, “Optimal ancilla-free Clifford+T approximation of z-rotations”, Quantum Inf. Comput. 16, 901 (2016), arXiv:1403.2975.
- decompose_step(op, qubits, bits, zvars)[source]¶
Decompose the Z-rotation into Clifford+T gates.
- Parameters:
- Returns:
A Circuit containing the Clifford+T approximation.
- Raises:
DecompositionError – If the operation is not a numeric
GateRZ.ValueError – If the angle is not finite.
- Return type:
- __init__(epsilon=1e-10, seed=0)¶
Modules
Canonical rewrite rule for decomposing operations to GateU and GateCX. |
|
Rewrite rule for flattening container operations. |
|
Ross–Selinger gridsynth: Clifford+T approximation of Z-rotations. |
|
Solovay-Kitaev algorithm for approximating arbitrary unitaries with Clifford+T. |
|
Special angle rewrite rule for Clifford+T decomposition. |
|
Rewrite rule for converting X and Y rotations to Z rotations. |
|
Rewrite rule for decomposing Toffoli into Clifford+T. |
|
ZYZ Euler angle decomposition rewrite rule. |