Skip to main contentIBM Quantum Documentation
This page is from an old version of Qiskit SDK. Go to the latest version

InstructionSet

class InstructionSet(circuit_cregs=None, *, resource_requester=None)

GitHub

Bases: object

Instruction collection, and their contexts.

New collection of instructions.

The context (qargs and cargs that each instruction is attached to) is also stored separately for each instruction.

Parameters

  • circuit_cregs (list[ClassicalRegister]) –

    Optional. List of cregs of the circuit to which the instruction is added. Default: None.

    Deprecated since version qiskit-terra

    0.19 The classical registers are insufficient to access all classical resources in a circuit, as there may be loose classical bits as well. It can also cause integer indices to be resolved incorrectly if any registers overlap. Instead, pass a complete requester to the resource_requester argument.

  • resource_requester (Optional[Callable]) –

    A callable that takes in the classical resource used in the condition, verifies that it is present in the attached circuit, resolves any indices into concrete Clbit instances, and returns the concrete resource. If this is not given, specifying a condition with an index is forbidden, and all concrete Clbit and ClassicalRegister resources will be assumed to be valid.

    Note

    The callback resource_requester is called once for each call to c_if(), and assumes that a call implies that the resource will now be used. It may throw an error if the resource is not valid for usage.

Raises

CircuitError – if both resource_requester and circuit_cregs are passed. Only one of these may be passed, and it should be resource_requester.


Methods

add

InstructionSet.add(instruction, qargs=None, cargs=None)

Add an instruction and its context (where it is attached).

c_if

InstructionSet.c_if(classical, val)

Set a classical equality condition on all the instructions in this set between the ClassicalRegister or Clbit classical and value val.

Note

This is a setter method, not an additive one. Calling this multiple times will silently override any previously set condition on any of the contained instructions; it does not stack.

Parameters

  • classical (Union[Clbit, ClassicalRegister, int]) – the classical resource the equality condition should be on. If this is given as an integer, it will be resolved into a Clbit using the same conventions as the circuit these instructions are attached to.
  • val (int) – the value the classical resource should be equal to.

Return type

InstructionSet

Returns

This same instance of InstructionSet, but now mutated to have the given equality condition.

Raises

CircuitError – if the passed classical resource is invalid, or otherwise not resolvable to a concrete resource that these instructions are permitted to access.

Example

from qiskit import ClassicalRegister, QuantumRegister, QuantumCircuit
 
qr = QuantumRegister(2)
cr = ClassicalRegister(2)
qc = QuantumCircuit(qr, cr)
qc.h(range(2))
qc.measure(range(2), range(2))
 
# apply x gate if the classical register has the value 2 (10 in binary)
qc.x(0).c_if(cr, 2)
 
# apply y gate if bit 0 is set to 1
qc.y(1).c_if(0, 1)
 
qc.draw()
      ┌───┐┌─┐    ┌───┐             
q0_0: ┤ H ├┤M├────┤ X ├─────────────
      ├───┤└╥┘┌─┐ └─╥─┘    ┌───┐    
q0_1: ┤ H ├─╫─┤M├───╫──────┤ Y ├────
      └───┘ ║ └╥┘   ║      └─╥─┘    
            ║  ║ ┌──╨──┐┌────╨─────┐
c0: 2/══════╩══╩═╡ 0x2 ╞╡ c0_0=0x1
            0  1 └─────┘└──────────┘

inverse

InstructionSet.inverse()

Invert all instructions.


Attributes

cargs

Legacy getter for the cargs components of an instruction set. This does not support mutation.

instructions

Legacy getter for the instruction components of an instruction set. This does not support mutation.

qargs

Legacy getter for the qargs components of an instruction set. This does not support mutation.

Was this page helpful?
Report a bug or request content on GitHub.