Skip to content

Commit

Permalink
fix: Run linter and fix formatting/complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
ltnln committed Jun 26, 2024
1 parent a55edb2 commit e39941d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
package_dir={"": "src"},
install_requires=[
"amazon-braket-schemas>=1.21.3",
"amazon-braket-default-simulator @ git+https://github.com/Altanali/amazon-braket-default-simulator-python.git@main",
"amazon-braket-default-simulator @ \
git+https://github.com/Altanali/amazon-braket-default-simulator-python.git@main",
"oqpy~=0.3.5",
"backoff",
"boltons",
Expand Down
9 changes: 7 additions & 2 deletions src/braket/circuits/braket_program_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.

from typing import Optional, Union
from collections.abc import Iterable
from typing import Optional, Union

import numpy as np
from sympy import Expr, Number
Expand Down Expand Up @@ -162,11 +162,16 @@ def handle_parameter_value(
return FreeParameterExpression(evaluated_value)
return value

def add_measure(self, target: tuple[int], classical_targets: Iterable[int] = None) -> None:
def add_measure(
self, target: tuple[int], classical_targets: Iterable[int] | None = None
) -> None:
"""Add a measure instruction to the circuit
Args:
target (tuple[int]): the target qubits to be measured.
classical_targets (Iterable[int] | None): the classical registers
to use in the qubit measurement.
"""
for iter, qubit in enumerate(target):
index = classical_targets[iter] if classical_targets else iter
Expand Down
13 changes: 5 additions & 8 deletions src/braket/circuits/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,14 +503,12 @@ def add_instruction(

# Check if there is a measure instruction on the circuit
self._check_if_qubit_measured(instruction, target, target_mapping)
#Update measure targets if instruction is a measurement

# Update measure targets if instruction is a measurement
if isinstance(instruction.operator, Measure):
if self._measure_targets:
self._measure_targets.append(target or instruction.target[0])
else:
self._measure_targets = [target or instruction.target[0]]

measure_target = target or instruction.target[0]
self._measure_targets = (self._measure_targets or []) + [measure_target]

if not target_mapping and not target:
# Nothing has been supplied, add instruction
instructions_to_add = [instruction]
Expand Down Expand Up @@ -718,7 +716,6 @@ def _add_measure(self, target_qubits: QubitSetInput) -> None:
)
)


def measure(self, target_qubits: QubitSetInput) -> Circuit:
"""
Add a `measure` operator to `self` ensuring only the target qubits are measured.
Expand Down
18 changes: 12 additions & 6 deletions test/unit_tests/braket/circuits/test_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -890,11 +890,16 @@ def test_from_ir_round_trip_transformation():
assert Circuit.from_ir(ir) == Circuit.from_ir(circuit.to_ir("OPENQASM"))
assert circuit.to_ir("OPENQASM") == Circuit.from_ir(ir).to_ir("OPENQASM")


def test_from_ir_round_trip_transformation_with_targeted_measurements():
circuit = Circuit().h(0).cnot(0, 1).\
add_instruction(Instruction(Measure(index=2), 1)).\
add_instruction(Instruction(Measure(index=1), 2)).\
add_instruction(Instruction(Measure(index=0), 0))
circuit = (
Circuit()
.h(0)
.cnot(0, 1)
.add_instruction(Instruction(Measure(index=2), 1))
.add_instruction(Instruction(Measure(index=1), 2))
.add_instruction(Instruction(Measure(index=0), 0))
)
ir = OpenQasmProgram(
source="\n".join(
[
Expand All @@ -904,8 +909,8 @@ def test_from_ir_round_trip_transformation_with_targeted_measurements():
"h q[0];",
"cnot q[0], q[1];",
"b[2] = measure q[1];",
"b[1] = measure q[2];",
"b[0] = measure q[0];"
"b[1] = measure q[2];",
"b[0] = measure q[0];",
]
),
inputs={},
Expand All @@ -914,6 +919,7 @@ def test_from_ir_round_trip_transformation_with_targeted_measurements():
assert Circuit.from_ir(ir) == Circuit.from_ir(circuit.to_ir("OPENQASM"))
assert circuit.to_ir("OPENQASM") == Circuit.from_ir(ir).to_ir("OPENQASM")


def test_add_with_instruction_with_default(cnot_instr):
circ = Circuit().add(cnot_instr)
assert circ == Circuit().add_instruction(cnot_instr)
Expand Down

0 comments on commit e39941d

Please sign in to comment.