Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/braket/circuits/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,25 @@ def __init__(self, addable: AddableTypes | None = None, *args, **kwargs):
if addable is not None:
self.add(addable, *args, **kwargs)

def count_instructions(self) -> dict[str, int]:
Comment thread
sim-eng-ii marked this conversation as resolved.
Outdated
"""Count the number of intructions in the circuit"""
count_dict = {}
for instr in self.instructions:
print(instr.operator)
Comment thread
sim-eng-ii marked this conversation as resolved.
Outdated
if instr.operator.name.lower() not in count_dict:
count_dict[instr.operator.name.lower()] = 1
else:
count_dict[instr.operator.name.lower()] += 1
return count_dict

def count_gates(self, gate_name: str) -> int:
"""Given a gate, count the number of times it appears in the circuit"""
count = 0
for instr in self.instructions:
if instr.operator.name.lower() == gate_name.lower():
count += 1
return count

@property
def depth(self) -> int:
"""int: Get the circuit depth."""
Expand Down
117 changes: 95 additions & 22 deletions test/unit_tests/braket/circuits/test_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@
)
from braket.circuits.translations import braket_result_to_result_type
from braket.ir.openqasm import Program as OpenQasmProgram
from braket.pulse import DragGaussianWaveform, Frame, GaussianWaveform, Port, PulseSequence
from braket.pulse import (
DragGaussianWaveform,
Frame,
GaussianWaveform,
Port,
PulseSequence,
)


@pytest.fixture
Expand Down Expand Up @@ -95,7 +101,11 @@ def port():
@pytest.fixture
def predefined_frame_1(port):
return Frame(
frame_id="predefined_frame_1", frequency=2e9, port=port, phase=0, is_predefined=True
frame_id="predefined_frame_1",
frequency=2e9,
port=port,
phase=0,
is_predefined=True,
)


Expand Down Expand Up @@ -431,6 +441,23 @@ def test_add_result_type_same_observable_wrong_target_order_hermitian():
assert not circ.basis_rotation_instructions


def test_count_instructions():
circ = Circuit().h(0).cnot(0, 1).rx(1, 0).measure(0)
expected = {
"h": 1,
"cnot": 1,
"rx": 1,
"measure": 1,
}
assert circ.count_instructions() == expected


def test_count_gates_cnot():
circ = Circuit().h(0).cnot(0, 1).rx(1, 0).measure(0)
expected = 1
assert circ.count_gates("cnot") == expected


def test_add_result_type_with_target_and_mapping(prob):
with pytest.raises(TypeError):
Circuit().add_result_type(prob, target=[10], target_mapping={0: 10})
Expand Down Expand Up @@ -1625,7 +1652,10 @@ def test_circuit_with_partial_calibrations(pulse_sequence_2):
serialization_properties = OpenQASMSerializationProperties(QubitReferenceType.VIRTUAL)
gate_calibrations = (
GateCalibrations({
(gates.MS(-0.1, FreeParameter("beta"), -0.3), QubitSet([0, 1])): pulse_sequence_2
(
gates.MS(-0.1, FreeParameter("beta"), -0.3),
QubitSet([0, 1]),
): pulse_sequence_2
}),
)
circuit.to_ir(
Expand Down Expand Up @@ -2591,7 +2621,10 @@ def test_to_unitary_with_global_phase():
(Circuit().y(1).z(2), np.kron(gates.Y().to_matrix(), gates.Z().to_matrix())),
(Circuit().rx(1, 0.15), gates.Rx(0.15).to_matrix()),
(Circuit().ry(1, 0.15).i(2), np.kron(gates.Ry(0.15).to_matrix(), np.eye(2))),
(Circuit().rz(1, 0.15).s(2), np.kron(gates.Rz(0.15).to_matrix(), gates.S().to_matrix())),
(
Circuit().rz(1, 0.15).s(2),
np.kron(gates.Rz(0.15).to_matrix(), gates.S().to_matrix()),
),
(Circuit().pswap(1, 2, 0.15), gates.PSwap(0.15).to_matrix()),
(Circuit().pswap(2, 1, 0.15), gates.PSwap(0.15).to_matrix()),
(Circuit().xy(1, 2, 0.15).i(3), np.kron(gates.XY(0.15).to_matrix(), np.eye(2))),
Expand All @@ -2604,18 +2637,30 @@ def test_to_unitary_with_global_phase():
(Circuit().ccnot(2, 1, 3), gates.CCNot().to_matrix()),
(Circuit().cswap(1, 2, 3).i(4), np.kron(gates.CSwap().to_matrix(), np.eye(2))),
(Circuit().cswap(1, 3, 2).i(4), np.kron(gates.CSwap().to_matrix(), np.eye(2))),
(Circuit().cswap(1, 2, 3).t(4), np.kron(gates.CSwap().to_matrix(), gates.T().to_matrix())),
(Circuit().cswap(1, 3, 2).t(4), np.kron(gates.CSwap().to_matrix(), gates.T().to_matrix())),
(
Circuit().cswap(1, 2, 3).t(4),
np.kron(gates.CSwap().to_matrix(), gates.T().to_matrix()),
),
(
Circuit().cswap(1, 3, 2).t(4),
np.kron(gates.CSwap().to_matrix(), gates.T().to_matrix()),
),
(Circuit().h(0).h(0), gates.I().to_matrix()),
(Circuit().h(0).x(0), np.dot(gates.X().to_matrix(), gates.H().to_matrix())),
(Circuit().x(0).h(0), np.dot(gates.H().to_matrix(), gates.X().to_matrix())),
(
Circuit().y(0).z(1).cnot(0, 1),
np.dot(gates.CNot().to_matrix(), np.kron(gates.Y().to_matrix(), gates.Z().to_matrix())),
np.dot(
gates.CNot().to_matrix(),
np.kron(gates.Y().to_matrix(), gates.Z().to_matrix()),
),
),
(
Circuit().z(0).y(1).cnot(0, 1),
np.dot(gates.CNot().to_matrix(), np.kron(gates.Z().to_matrix(), gates.Y().to_matrix())),
np.dot(
gates.CNot().to_matrix(),
np.kron(gates.Z().to_matrix(), gates.Y().to_matrix()),
),
),
(
Circuit().y(0).z(1).cnot(0, 1).cnot(1, 2),
Expand Down Expand Up @@ -3003,7 +3048,8 @@ def test_basis_rotation_instructions_tensor_product():
.h(0)
.cnot(0, 1)
.expectation(
observable=observables.X() @ observables.Y() @ observables.Y(), target=[0, 1, 2]
observable=observables.X() @ observables.Y() @ observables.Y(),
target=[0, 1, 2],
)
)
expected = [
Expand All @@ -3024,7 +3070,8 @@ def test_basis_rotation_instructions_tensor_product_shared_factors():
.h(0)
.cnot(0, 1)
.expectation(
observable=observables.X() @ observables.Y() @ observables.Y(), target=[0, 1, 2]
observable=observables.X() @ observables.Y() @ observables.Y(),
target=[0, 1, 2],
)
.expectation(observable=observables.X() @ observables.Y(), target=[0, 1])
)
Expand Down Expand Up @@ -3101,7 +3148,10 @@ def test_basis_rotation_instructions_multiple_result_types_all_specified_same_ta
.expectation(observable=observables.H())
.sample(observable=observables.H(), target=[0])
)
expected = [Instruction(gates.Ry(-np.pi / 4), 0), Instruction(gates.Ry(-np.pi / 4), 1)]
expected = [
Instruction(gates.Ry(-np.pi / 4), 0),
Instruction(gates.Ry(-np.pi / 4), 1),
]
assert circ.basis_rotation_instructions == expected


Expand All @@ -3113,7 +3163,10 @@ def test_basis_rotation_instructions_multiple_result_types_specified_all_same_ta
.sample(observable=observables.H(), target=[0])
.expectation(observable=observables.H())
)
expected = [Instruction(gates.Ry(-np.pi / 4), 0), Instruction(gates.Ry(-np.pi / 4), 1)]
expected = [
Instruction(gates.Ry(-np.pi / 4), 0),
Instruction(gates.Ry(-np.pi / 4), 1),
]
assert circ.basis_rotation_instructions == expected


Expand All @@ -3122,9 +3175,13 @@ def test_basis_rotation_instructions_multiple_result_types_same_targets_hermitia
Circuit()
.h(0)
.cnot(0, 1)
.sample(observable=observables.Hermitian(matrix=np.array([[1, 0], [0, -1]])), target=[1])
.sample(
observable=observables.Hermitian(matrix=np.array([[1, 0], [0, -1]])),
target=[1],
)
.expectation(
observable=observables.Hermitian(matrix=np.array([[1, 0], [0, -1]])), target=[1]
observable=observables.Hermitian(matrix=np.array([[1, 0], [0, -1]])),
target=[1],
)
)
expected = [Instruction(gates.Unitary(matrix=np.array([[0, 1], [1, 0]])), target=[1])]
Expand All @@ -3136,9 +3193,13 @@ def test_basis_rotation_instructions_multiple_result_types_different_hermitian_t
Circuit()
.h(0)
.cnot(0, 1)
.sample(observable=observables.Hermitian(matrix=np.array([[1, 0], [0, -1]])), target=[1])
.sample(
observable=observables.Hermitian(matrix=np.array([[1, 0], [0, -1]])),
target=[1],
)
.expectation(
observable=observables.Hermitian(matrix=np.array([[0, 1], [1, 0]])), target=[0]
observable=observables.Hermitian(matrix=np.array([[0, 1], [1, 0]])),
target=[0],
)
)
expected = [
Expand Down Expand Up @@ -3168,7 +3229,8 @@ def test_basis_rotation_instructions_multiple_result_types_tensor_product_hermit
target=[0, 1],
)
.expectation(
observable=observables.Hermitian(matrix=np.array([[0, 1], [1, 0]])), target=[2]
observable=observables.Hermitian(matrix=np.array([[0, 1], [1, 0]])),
target=[2],
)
)
expected = [
Expand All @@ -3192,7 +3254,8 @@ def test_basis_rotation_instructions_multiple_result_types_tensor_product_hermit
.cnot(1, 2)
.expectation(observable=observables.I(), target=[1])
.sample(
observable=observables.Hermitian(matrix=np.eye(4)) @ observables.H(), target=[0, 1, 2]
observable=observables.Hermitian(matrix=np.eye(4)) @ observables.H(),
target=[0, 1, 2],
)
.variance(observable=observables.H(), target=[2])
.variance(observable=observables.Hermitian(matrix=np.eye(4)), target=[0, 1])
Expand All @@ -3212,7 +3275,10 @@ def test_basis_rotation_instructions_multiple_result_types_tensor_product_probab
.cnot(0, 1)
.cnot(1, 2)
.probability([0, 1])
.sample(observable=observables.Z() @ observables.Z() @ observables.H(), target=[0, 1, 2])
.sample(
observable=observables.Z() @ observables.Z() @ observables.H(),
target=[0, 1, 2],
)
.variance(observable=observables.H(), target=[2])
)
expected = [
Expand Down Expand Up @@ -3568,7 +3634,8 @@ def test_pulse_circuit_to_openqasm(predefined_frame_1, user_defined_frame):
)

pulse_sequence_2.play(
user_defined_frame, GaussianWaveform(length=1e-3, sigma=0.7, id="gauss_wf_ignore")
user_defined_frame,
GaussianWaveform(length=1e-3, sigma=0.7, id="gauss_wf_ignore"),
)

assert circuit.to_ir(
Expand Down Expand Up @@ -3639,7 +3706,10 @@ def test_pulse_circuit_conflicting_frame(user_defined_frame):
pulse_sequence_user_defined_frame_x = (
PulseSequence()
.set_frequency(user_defined_frame_x, 3e9)
.play(user_defined_frame_x, GaussianWaveform(length=1e-3, sigma=0.7, id="gauss_wf"))
.play(
user_defined_frame_x,
GaussianWaveform(length=1e-3, sigma=0.7, id="gauss_wf"),
)
)

pulse_sequence_user_defined_frame = (
Expand Down Expand Up @@ -3673,7 +3743,10 @@ def test_parametrized_pulse_circuit(user_defined_frame):
pulse_sequence = (
PulseSequence()
.set_frequency(user_defined_frame, frequency_parameter)
.play(user_defined_frame, GaussianWaveform(length=length, sigma=0.7, id="gauss_wf"))
.play(
user_defined_frame,
GaussianWaveform(length=length, sigma=0.7, id="gauss_wf"),
)
)

circuit = (
Expand Down
Loading