mimiqcircuits.operations.block¶
Block operation for grouping instructions.
Classes
|
Reusable group of instructions, applied as a single operation. |
- class Block(*args, num_ancillas=0)[source]¶
Bases:
OperationReusable group of instructions, applied as a single operation.
A block holds a list of instructions on its own local qubits, bits and z-variables, numbered from 0. Pushing a block to a circuit maps those local indices to the targets given to
push, in the order qubits, bits, z-variables. Decomposing the circuit replaces the block with its instructions.The constructor accepts:
Block(): an empty block with no qubits, bits or z-variables.Block(circuit): a copy of the instructions of a circuit or block. The sizes are inferred from the highest index used.Block(instructions): a block from a list ofInstructionobjects, with sizes inferred the same way.Block(num_qubits, num_bits, num_zvars): an empty block of the given sizes, to be filled withpush().Block(num_qubits, num_bits, num_zvars, instructions): a block of the given sizes holdinginstructions.
Every form takes a
num_ancillaskeyword (default 0): the number of its last qubits that are clean ancillas, found and returned in|0>.The instructions are deep-copied, so later changes to the source circuit do not affect the block. The sizes are fixed once the block is created.
- Raises:
TypeError – If an argument has the wrong type, for example a list holding something other than instructions.
ValueError – If the number of arguments is not 0, 1, 3 or 4.
Examples
>>> from mimiqcircuits import * >>> c = Circuit()
>>> c.push(GateCX(), 1, 2) 3-qubit circuit with 1 instruction: └── CX @ q[1], q[2]
>>> c.push(GateCX(), 1, 3) 4-qubit circuit with 2 instructions: ├── CX @ q[1], q[2] └── CX @ q[1], q[3]
>>> c.push(MeasureZZ(), 1, 2, 1) 4-qubit, 2-bit circuit with 3 instructions: ├── CX @ q[1], q[2] ├── CX @ q[1], q[3] └── MZZ @ q[1:2], c[1]
Each block prints with a unique identifier, shown as
...here.>>> block = Block(c) >>> block 4-qubit, 2-bit block ... with 3 instructions: ├── CX @ q[1], q[2] ├── CX @ q[1], q[3] └── MZZ @ q[1:2], c[1]
>>> main = Circuit() >>> main.push(block, 0, 1, 2, 3, 0, 1) 4-qubit, 2-bit circuit with 1 instruction: └── block ... @ q[0,1,2,3], c[0,1]
Decomposing expands the block, and decomposes
MeasureZZfurther:>>> main.decompose() 4-qubit, 2-bit circuit with 5 instructions: ├── CX @ q[1], q[2] ├── CX @ q[1], q[3] ├── CX @ q[1], q[2] ├── M @ q[2], c[1] └── CX @ q[1], q[2]
A block can also be built empty with fixed sizes and filled with
push:>>> bell = Block(2, 0, 0) >>> _ = bell.push(GateH(), 0) >>> bell.push(GateCX(), 0, 1) 2-qubit block ... with 2 instructions: ├── H @ q[0] └── CX @ q[0], q[1]
See also
Instruction,Circuit- property num_ancillas¶
Number of clean ancillas, the last qubit targets of the block.
- push(operation, *args)[source]¶
Add an operation to the end of the block.
Works like
Circuit.push(), but every target must fit inside the block’s fixed sizes.- Parameters:
operation – The operation, or an
Instruction.*args – The targets: qubits, then bits, then z-variables.
- Returns:
The block itself, so calls can be chained.
- Return type:
- Raises:
ValueError – If a target index is not smaller than the block’s number of qubits, bits or z-variables.
- is_symbolic()[source]¶
Whether any parameter contains a free symbol.
For wrappers such as
ControlorPower, the wrapped operation is checked too.- Returns:
Trueif at least one parameter is symbolic.- Return type:
Examples
>>> from mimiqcircuits import * >>> from symengine import Symbol >>> GateRX(Symbol("theta")).is_symbolic() True >>> GateRX(0.5).is_symbolic() False
- listvars()[source]¶
List the symbolic variables of all the instructions of the block.
- Returns:
Each
symengine.Symbolonce, in no particular order.- Return type:
- evaluate(d)[source]¶
Substitute values for the symbols in the operation’s parameters.
Operations without symbolic parameters may return themselves.
- supports_canonical_rewrite()[source]¶
Whether
CanonicalRewritemay decompose this operation.Operations opt in: only those the canonical rewrite knows how to handle return
True.- Returns:
bool
- __call__(circuit, *targets)[source]¶
Push the block’s instructions, mapped to
targets, tocircuit.Unlike pushing the block itself, this inlines its instructions.
- Parameters:
circuit – The circuit to add the instructions to.
*targets – Qubits, then bits, then z-variables, one for each of the block’s own.
- Returns:
The circuit.
- Raises:
ValueError – If the number of targets does not match the block.