braket.circuits.moments module

class braket.circuits.moments.MomentType(value)[source]

Bases: str, Enum

The type of moments. GATE: a gate NOISE: a noise channel added directly to the circuit GATE_NOISE: a gate-based noise channel INITIALIZATION_NOISE: a initialization noise channel READOUT_NOISE: a readout noise channel COMPILER_DIRECTIVE: an instruction to the compiler, external to the quantum program itself MEASURE: a measurement

GATE = 'gate'
NOISE = 'noise'
GATE_NOISE = 'gate_noise'
INITIALIZATION_NOISE = 'initialization_noise'
READOUT_NOISE = 'readout_noise'
COMPILER_DIRECTIVE = 'compiler_directive'
GLOBAL_PHASE = 'global_phase'
MEASURE = 'measure'
class braket.circuits.moments.MomentsKey(time: int, qubits: QubitSet, moment_type: MomentType, noise_index: int, subindex: int = 0)[source]

Bases: NamedTuple

Key of the Moments mapping.

Parameters:
  • time – moment

  • qubits – qubit set

  • moment_type – The type of the moment

  • noise_index – the number of noise channels at the same moment. For gates, this is the number of gate_noise channels associated with that gate. For all other noise types, noise_index starts from 0; but for gate noise, it starts from 1.

Create new instance of MomentsKey(time, qubits, moment_type, noise_index, subindex)

time: int

Alias for field number 0

qubits: QubitSet

Alias for field number 1

moment_type: MomentType

Alias for field number 2

noise_index: int

Alias for field number 3

subindex: int

Alias for field number 4

class braket.circuits.moments.Moments(instructions: Iterable[Instruction] | None = None)[source]

Bases: Mapping[MomentsKey, Instruction]

An ordered mapping of MomentsKey or NoiseMomentsKey to Instruction. The core data structure that contains instructions, ordering they are inserted in, and time slices when they occur. Moments implements Mapping and functions the same as a read-only dictionary. It is mutable only through the add() method.

This data structure is useful to determine a dependency of instructions, such as printing or optimizing circuit structure, before sending it to a quantum device. The original insertion order is preserved and can be retrieved via the values() method.

Parameters:

instructions (Iterable[Instruction] | None) – Instructions to initialize self. Default = None.

Examples

>>> moments = Moments()
>>> moments.add([Instruction(Gate.H(), 0), Instruction(Gate.CNot(), [0, 1])])
>>> moments.add([Instruction(Gate.H(), 0), Instruction(Gate.H(), 1)])
>>> for i, item in enumerate(moments.items()):
...     print(f"Item {i}")
...     print(f"\\tKey: {item[0]}")
...     print(f"\\tValue: {item[1]}")
...
Item 0
    Key: MomentsKey(time=0, qubits=QubitSet([Qubit(0)]))
    Value: Instruction('operator': H, 'target': QubitSet([Qubit(0)]))
Item 1
    Key: MomentsKey(time=1, qubits=QubitSet([Qubit(0), Qubit(1)]))
    Value: Instruction('operator': CNOT, 'target': QubitSet([Qubit(0), Qubit(1)]))
Item 2
    Key: MomentsKey(time=2, qubits=QubitSet([Qubit(0)]))
    Value: Instruction('operator': H, 'target': QubitSet([Qubit(0)]))
Item 3
    Key: MomentsKey(time=2, qubits=QubitSet([Qubit(1)]))
    Value: Instruction('operator': H, 'target': QubitSet([Qubit(1)]))
property depth: int

Get the depth (number of slices) of self.

Type:

int

property qubit_count: int

Get the number of qubits used across all of the instructions.

Type:

int

property qubits: QubitSet

Get the qubits used across all of the instructions. The order of qubits is based on the order in which the instructions were added.

Note

Don’t mutate this object, any changes may impact the behavior of this class and / or consumers. If you need to mutate this, then copy it via QubitSet(moments.qubits()).

Type:

QubitSet

time_slices() dict[int, list[Instruction]][source]

Get instructions keyed by time.

Returns:

dict[int, list[Instruction]] – Key is the time and value is a list of instructions that occur at that moment in time. The order of instructions is in no particular order.

Note

This is a computed result over self and can be freely mutated. This is re-computed with every call, with a computational runtime O(N) where N is the number of instructions in self.

add(instructions: Iterable[Instruction] | Instruction, noise_index: int = 0) None[source]

Add one or more instructions to self.

Parameters:
  • instructions (Union[Iterable[Instruction], Instruction]) – Instructions to add to self. The instruction is added to the max time slice in which the instruction fits.

  • noise_index (int) – the number of noise channels at the same moment. For gates, this is the number of gate_noise channels associated with that gate. For all other noise types, noise_index starts from 0; but for gate noise, it starts from 1.

add_noise(instruction: Instruction, input_type: str = 'noise', noise_index: int = 0) None[source]

Adds noise to a moment.

Parameters:
  • instruction (Instruction) – Instruction to add.

  • input_type (str) – One of MomentType.

  • noise_index (int) – The number of noise channels at the same moment. For gates, this is the number of gate_noise channels associated with that gate. For all other noise types, noise_index starts from 0; but for gate noise, it starts from 1.

sort_moments() None[source]

Make the disordered moments in order.

  1. Make the readout noise in the end

  2. Make the initialization noise at the beginning

keys() KeysView[MomentsKey][source]

Return a view of self’s keys.

items() ItemsView[MomentsKey, Instruction][source]

Return a view of self’s (key, instruction).

values() ValuesView[Instruction][source]

Return a view of self’s instructions.

Returns:

ValuesView[Instruction] – The (in-order) instructions.

get(key: MomentsKey, default: Any | None = None) Instruction[source]

Get the instruction in self by key.

Parameters:
  • key (MomentsKey) – Key of the instruction to fetch.

  • default (Any | None) – Value to return if key is not in moments. Default = None.

Returns:

Instructionmoments[key] if key in moments, else default is returned.