Skip to content

Commit

Permalink
test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
speller26 committed Aug 19, 2024
1 parent dc6a99b commit 49829e7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
2 changes: 0 additions & 2 deletions src/braket/circuits/observables.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,8 +606,6 @@ def __init__(
raise ValueError(f"{self._matrix} is not hermitian")

qubit_count = int(np.log2(self._matrix.shape[0]))
if targets and len(targets) != qubit_count:
raise ValueError(f"Target size {len(targets)} does not match qubit count {qubit_count}")
eigendecomposition = Hermitian._get_eigendecomposition(self._matrix)
self._eigenvalues = eigendecomposition["eigenvalues"]
self._diagonalizing_gates = (
Expand Down
34 changes: 20 additions & 14 deletions test/unit_tests/braket/circuits/test_observables.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,20 +237,6 @@ def test_to_ir(testobject, gateobject, expected_ir, basis_rotation_gates, eigenv
"hermitian([[1+0im, 0im, 0im, 0im], [0im, 1+0im, 0im, 0im], "
"[0im, 0im, 1+0im, 0im], [0im, 0im, 0im, 1+0im]]) $0, $1",
),
# (
# (2 * Observable.Z()) @ (3 * Observable.H()),
# (2 * Observable.Z(3)) @ (3 * Observable.H(3)),
# OpenQASMSerializationProperties(qubit_reference_type=QubitReferenceType.PHYSICAL),
# [3, 3],
# "6 * z($3) @ h($3)",
# ),
# (
# (2 * Observable.Z()) @ (3 * Observable.H()) @ (2 * Observable.Y()),
# (2 * Observable.Z(3)) @ (3 * Observable.H(3)) @ (2 * Observable.Y(1)),
# OpenQASMSerializationProperties(qubit_reference_type=QubitReferenceType.PHYSICAL),
# [3, 3, 1],
# "12 * z($3) @ h($3) @ y($1)",
# ),
(
3 * (2 * Observable.Z()),
3 * (2 * Observable.Z(3)),
Expand Down Expand Up @@ -508,6 +494,11 @@ def test_hermitian_eigenvalues(matrix, eigenvalues):
compare_eigenvalues(Observable.Hermitian(matrix=matrix), eigenvalues)


def test_hermitian_matrix_target_mismatch():
with pytest.raises(ValueError):
Observable.Hermitian(np.eye(4), targets=[0, 1, 2])


def test_flattened_tensor_product():
observable_one = Observable.Z() @ Observable.Y()
observable_two = Observable.X() @ Observable.H()
Expand Down Expand Up @@ -662,6 +653,16 @@ def test_tensor_product_basis_rotation_gates(observable, basis_rotation_gates):
assert observable.basis_rotation_gates == basis_rotation_gates


def test_tensor_product_repeated_qubits():
with pytest.raises(ValueError):
(2 * Observable.Z(3)) @ (3 * Observable.H(3))


def test_tensor_product_with_and_without_targets():
with pytest.raises(ValueError):
(2 * Observable.Z(3)) @ (3 * Observable.H())


def test_observable_from_ir_tensor_product():
expected_observable = Observable.TensorProduct([Observable.Z(), Observable.I(), Observable.X()])
actual_observable = observable_from_ir(["z", "i", "x"])
Expand Down Expand Up @@ -764,3 +765,8 @@ def test_unscaled_tensor_product():
observable = 3 * ((2 * Observable.X()) @ (5 * Observable.Y()))
assert observable == 30 * (Observable.X() @ Observable.Y())
assert observable._unscaled() == Observable.X() @ Observable.Y()


def test_sum_with_and_without_targets():
with pytest.raises(ValueError):
3 * ((2 * Observable.X()) @ (5 * Observable.Y(4)))

0 comments on commit 49829e7

Please sign in to comment.