mimiqcircuits.decomposition.basis.rule_basis

RuleBasis: Wrapper to use a RewriteRule as a DecompositionBasis.

Classes

RuleBasis(rule)

Decomposition basis that applies one rewrite rule until it no longer matches.

class RuleBasis(rule)[source]

Bases: DecompositionBasis

Decomposition basis that applies one rewrite rule until it no longer matches.

An operation is terminal when the rule does not match it. When the rule does not match an IfStatement, it is applied to the body, which stays conditional. decompose() and eachdecomposed() wrap a rule passed in place of a basis in a RuleBasis.

Parameters:

rule (RewriteRule) – The rewrite rule to apply.

Examples

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

>>> c.push(GateRX(pi/2), 0)
1-qubit circuit with 2 instructions:
├── H @ q[0]
└── RX((1/2)*pi) @ q[0]

>>> # rewrite every X and Y rotation into Z rotations; H is kept
>>> decompose(c, RuleBasis(ToZRotationRewrite()))
1-qubit circuit with 4 instructions:
├── H @ q[0]
├── H @ q[0]
├── RZ((1/2)*pi) @ q[0]
└── H @ q[0]
isterminal(op)[source]

Check whether the rule leaves an operation alone.

Parameters:

op (Operation) – The operation to check.

Returns:

True if the rule does not match op. For an IfStatement the rule does not match, the answer for its body.

Return type:

bool

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

Apply the rule once to an operation.

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 the rewrite. For an IfStatement the rule does not match, the rewrite of its body, with each instruction made conditional.

Raises:

DecompositionError – If the rule cannot rewrite op.

Return type:

Circuit

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

Apply the rule once, with a context, as decompose() does.