braket.circuits.instruction module

class braket.circuits.instruction.Instruction(operator: InstructionOperator, target: QubitSetInput | None = None, *, control: QubitSetInput | None = None, control_state: BasisStateInput | None = None, power: float = 1)[source]

Bases: object

An instruction is a quantum directive that describes the quantum task to perform on a quantum device.

InstructionOperator includes objects of type Gate and Noise only.

Parameters:
  • operator (InstructionOperator) – Operator for the instruction.

  • target (Optional[QubitSetInput]) – Target qubits that the operator is applied to. Default is None.

  • control (Optional[QubitSetInput]) – Target qubits that the operator is controlled on. Default is None.

  • control_state (Optional[BasisStateInput]) – Quantum state on which to control the operation. Must be a binary sequence of same length as number of qubits in control. Will be ignored if control is not present. May be represented as a string, list, or int. For example “0101”, [0, 1, 0, 1], 5 all represent controlling on qubits 0 and 2 being in the |0⟩ state and qubits 1 and 3 being in the |1⟩ state. Default “1” * len(control).

  • power (float) – Integer or fractional power to raise the gate to. Negative powers will be split into an inverse, accompanied by the positive power. Default 1.

Raises:
  • ValueError – If operator is empty or any integer in target does not meet the Qubit or QubitSet class requirements. Also, if operator qubit count does not equal the size of the target qubit set.

  • TypeError – If a Qubit class can’t be constructed from target due to an incorrect typing.

Examples

>>> Instruction(Gate.CNot(), [0, 1])
Instruction('operator': CNOT, 'target': QubitSet(Qubit(0), Qubit(1)))
>>> instr = Instruction(Gate.CNot()), QubitSet([0, 1])])
Instruction('operator': CNOT, 'target': QubitSet(Qubit(0), Qubit(1)))
>>> instr = Instruction(Gate.H(), 0)
Instruction('operator': H, 'target': QubitSet(Qubit(0),))
>>> instr = Instruction(Gate.Rx(0.12), 0)
Instruction('operator': Rx, 'target': QubitSet(Qubit(0),))
>>> instr = Instruction(Gate.Rx(0.12, control=1), 0)
Instruction(
    'operator': Rx,
    'target': QubitSet(Qubit(0),),
    'control': QubitSet(Qubit(1),),
)
property operator: Operator

The operator for the instruction, for example, Gate.

Type:

Operator

property target: QubitSet

Target qubits that the operator is applied to.

Type:

QubitSet

property control: QubitSet

Target qubits that the operator is controlled on.

Type:

QubitSet

property control_state: BasisState

Quantum state that the operator is controlled to.

Type:

BasisState

property power: float

Power that the operator is raised to.

Type:

float

adjoint() list[Instruction][source]

Returns a list of Instructions implementing adjoint of this instruction’s own operator

This operation only works on Gate operators and compiler directives.

Returns:

list[Instruction] – A list of new instructions that comprise the adjoint of this operator

Raises:

NotImplementedError – If operator is not of type Gate or CompilerDirective

to_ir(ir_type: IRType = IRType.JAQCD, serialization_properties: SerializationProperties | None = None) Any[source]

Converts the operator into the canonical intermediate representation. If the operator is passed in a request, this method is called before it is passed.

Parameters:
  • ir_type (IRType) – The IRType to use for converting the instruction object to its IR representation.

  • serialization_properties (SerializationProperties | None) – The serialization properties to use while serializing the object to the IR representation. The serialization properties supplied must correspond to the supplied ir_type. Defaults to None.

Returns:

Any – IR object of the instruction.

property ascii_symbols: tuple[str, ...]

Returns the ascii symbols for the instruction’s operator.

Type:

tuple[str, …]

copy(target_mapping: dict[QubitInput, QubitInput] | None = None, target: QubitSetInput | None = None, control_mapping: dict[QubitInput, QubitInput] | None = None, control: QubitSetInput | None = None, control_state: BasisStateInput | None = None, power: float = 1) Instruction[source]

Return a shallow copy of the instruction.

Note

If target_mapping is specified, then self.target is mapped to the specified qubits. This is useful apply an instruction to a circuit and change the target qubits. Same relationship holds for control_mapping.

Parameters:
  • target_mapping (Optional[dict[QubitInput, QubitInput]]) – A dictionary of qubit mappings to apply to the target. Key is the qubit in this target and the value is what the key is changed to. Default = None.

  • target (Optional[QubitSetInput]) – Target qubits for the new instruction. Default is None.

  • control_mapping (Optional[dict[QubitInput, QubitInput]]) – A dictionary of qubit mappings to apply to the control. Key is the qubit in this control and the value is what the key is changed to. Default = None.

  • control (Optional[QubitSetInput]) – Control qubits for the new instruction. Default is None.

  • control_state (Optional[BasisStateInput]) – Quantum state on which to control the operation. Must be a binary sequence of same length as number of qubits in control. Will be ignored if control is not present. May be represented as a string, list, or int. For example “0101”, [0, 1, 0, 1], 5 all represent controlling on qubits 0 and 2 being in the |0⟩ state and qubits 1 and 3 being in the |1⟩ state. Default “1” * len(control).

  • power (float) – Integer or fractional power to raise the gate to. Negative powers will be split into an inverse, accompanied by the positive power. Default 1.

Returns:

Instruction – A shallow copy of the instruction.

Raises:

TypeError – If both target_mapping and target are supplied.

Examples

>>> instr = Instruction(Gate.H(), 0)
>>> new_instr = instr.copy()
>>> new_instr.target
QubitSet(Qubit(0))
>>> new_instr = instr.copy(target_mapping={0: 5})
>>> new_instr.target
QubitSet(Qubit(5))
>>> new_instr = instr.copy(target=[5])
>>> new_instr.target
QubitSet(Qubit(5))