mimiqcircuits.BlockDecl

class BlockDecl(name, arguments, body, num_ancillas=None)[source]

Bases: object

Declaration of a named block with symbolic arguments.

A BlockDecl is to Block what GateDecl is to a gate: a reusable body that can hold any instruction (measurements, resets, noise, conditionals, z-variable operations) and takes arguments. A declaration cannot be pushed to a circuit; calling it with values gives a BlockCall, which can.

Every symbol used in the body must be one of the arguments, so evaluating a call only changes the call’s values and all calls keep sharing the same declaration.

Parameters:
  • name (str) – Name of the block.

  • arguments (tuple) – The symbolic arguments, as symengine.Symbol.

  • body – A Circuit, a Block or a list of Instruction. A Block keeps its declared sizes; for the others the sizes are those used by the instructions.

  • num_ancillas (int) – How many of the last qubits of each call are clean ancillas, found and returned in |0>. Defaults to that of body when it is a Block, and to 0 otherwise.

Raises:
  • TypeError – If an argument is not a symbol.

  • ValueError – If the arguments repeat, the body uses a symbol that is not an argument, or num_ancillas is negative or larger than the number of qubits.

Examples

Declare a block that rotates a qubit, entangles it with a second one and measures the second one.

>>> from symengine import symbols
>>> from mimiqcircuits import *
>>> theta = symbols("theta")
>>> body = Circuit()
>>> _ = body.push(GateRX(theta), 0)
>>> _ = body.push(GateCX(), 0, 1)
>>> _ = body.push(Measure(), 1, 0)
>>> syndrome = BlockDecl("syndrome", (theta,), body)
>>> syndrome
block syndrome(theta) =
├── RX(theta) @ q[0]
├── CX @ q[0], q[1]
└── M @ q[1], c[0]

Calling the declaration gives a BlockCall, which is pushed like any other operation: here on qubits 0 and 1 with bit 0, then on qubits 3 and 2 with bit 1. Both calls refer to the same declaration.

>>> c = Circuit()
>>> _ = c.push(syndrome(0.1), 0, 1, 0)
>>> _ = c.push(syndrome(0.2), 3, 2, 1)
>>> c
4-qubit, 2-bit circuit with 2 instructions:
├── syndrome(0.1) @ q[0], q[1], c[0]
└── syndrome(0.2) @ q[3], q[2], c[1]

One decomposition step replaces each call with its body, with the values in place of the arguments.

>>> decompose_step(c)
4-qubit, 2-bit circuit with 6 instructions:
├── RX(0.1) @ q[0]
├── CX @ q[0], q[1]
├── M @ q[1], c[0]
├── RX(0.2) @ q[3]
├── CX @ q[3], q[2]
└── M @ q[2], c[1]
__init__(name, arguments, body, num_ancillas=None)[source]

Methods

__init__(name, arguments, body[, num_ancillas])

Attributes

num_ancillas

Number of clean ancillas, the last qubit targets of each call.

num_bits

Number of classical bits a call to this block acts on.

num_qubits

Number of qubits a call to this block acts on.

num_zvars

Number of z-variables a call to this block acts on.

property num_qubits

Number of qubits a call to this block acts on.

property num_ancillas

Number of clean ancillas, the last qubit targets of each call.

property num_bits

Number of classical bits a call to this block acts on.

property num_zvars

Number of z-variables a call to this block acts on.