mimiqcircuits.decomposition.rules.flatten_containers

Rewrite rule for flattening container operations.

Classes

FlattenContainers()

Rewrite rule that expands container operations into their contents.

class FlattenContainers[source]

Bases: RewriteRule

Rewrite rule that expands container operations into their contents.

Matches GateCall, Block, BlockCall, and the Inverse of a GateCall, and replaces each by the instructions it holds (inverted for an Inverse), remapped onto the targets. One step expands one level: nested containers stay as they are. Gates are never decomposed. FlattenedBasis repeats this rule until no container is left.

Examples

>>> from mimiqcircuits import *
>>> # declare a gate that prepares a Bell pair, and call it
>>> bell = GateDecl("bell", (), Circuit().push(GateH(), 0).push(GateCX(), 0, 1))
>>> c = Circuit()
>>> c.push(bell(), 2, 3)
4-qubit circuit with 1 instruction:
└── bell() @ q[2:3]

>>> # the call is replaced by its body, on qubits 2 and 3
>>> decompose_step(c, FlattenContainers())
4-qubit circuit with 2 instructions:
├── H @ q[2]
└── CX @ q[2], q[3]
matches(op)[source]

Check whether an operation is a container this rule expands.

Parameters:

op (Operation) – The operation to check.

Returns:

True for GateCall, Block, BlockCall, and the Inverse of a GateCall.

Return type:

bool

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

Expand one level of a container operation.

Parameters:
  • op (Operation) – The container to expand.

  • qubits (Sequence[int]) – Qubit indices the container acts on.

  • bits (Sequence[int]) – Classical bit indices the container acts on.

  • zvars (Sequence[int]) – Z-variable indices the container acts on.

Returns:

A new Circuit with the contents of op on those indices.

Raises:

DecompositionError – If op is not a container this rule matches.

Return type:

Circuit