braket.circuits.moments module

class braket.circuits.moments.MomentsKey[source]

Bases: tuple

Key of the Moments mapping.

Create new instance of MomentsKey(time, qubits)

property time

Alias for field number 0

property qubits

Alias for field number 1

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

Bases: collections.abc.Mapping, typing.Generic

An ordered mapping of MomentsKey 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], optional) – Instructions to initialize self with. Default = [].

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

Get the depth (number of slices) of self.

Type

int

property qubit_count

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

Type

int

property qubits

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[braket.circuits.instruction.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[braket.circuits.instruction.Instruction]) → None[source]

Add instructions to self.

Parameters
  • instructions (Iterable[Instruction]) – Instructions to add to self. The instruction

  • added to the max time slice in which the instruction fits. (are) –

keys() → KeysView[braket.circuits.moments.MomentsKey][source]

Return a view of self’s keys.

items() → ItemsView[braket.circuits.moments.MomentsKey, braket.circuits.instruction.Instruction][source]

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

values() → ValuesView[braket.circuits.instruction.Instruction][source]

Return a view of self’s instructions.

get(key: braket.circuits.moments.MomentsKey, default=None)braket.circuits.instruction.Instruction[source]

Get the instruction in self by key.

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

  • default (Any, optional) – Value to return if key is not in moment. Default = None.

Returns

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