Skip to content

Commit fda578a

Browse files
committed
add missing tests for errors and grover
1 parent 941dd6d commit fda578a

File tree

4 files changed

+51
-2
lines changed

4 files changed

+51
-2
lines changed

src/isotropic/algos/grover.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def get_grover_answer(result: PrimitiveResult) -> tuple[dict, str]:
7777
A tuple containing the counts dictionary and the most probable search item.
7878
"""
7979
if not isinstance(result, PrimitiveResult):
80-
raise ValueError("The result must be an instance of PrimitiveResult.")
80+
raise TypeError("The result must be an instance of PrimitiveResult.")
8181

8282
counts = result[0].data.c0.get_counts()
8383
grover_answer = max(counts, key=counts.get)

src/isotropic/utils/distribution.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def double_factorial_ratio_jax(num: int, den: int) -> Array:
8080
For very large numbers, this is numerically stable only when |num - den| is ~5.
8181
"""
8282
warnings.warn(
83-
"This is an experimental implementation. There are known issues when with using this for numbers larger than 2**8",
83+
"This is an experimental implementation. There are known issues with using this for numbers larger than 2**8",
8484
UserWarning,
8585
)
8686
if abs(num - den) > 4:

tests/test_grover.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import pytest
2+
from qiskit.primitives import StatevectorSampler
3+
from qiskit.quantum_info import Operator
4+
5+
from isotropic.algos.grover import (
6+
get_grover_answer,
7+
get_grover_circuit,
8+
optimal_num_iterations,
9+
)
10+
11+
U_w = Operator(
12+
[
13+
[1, 0, 0, 0, 0, 0, 0, 0],
14+
[0, 1, 0, 0, 0, 0, 0, 0],
15+
[0, 0, 1, 0, 0, 0, 0, 0],
16+
[0, 0, 0, -1, 0, 0, 0, 0],
17+
[0, 0, 0, 0, 1, 0, 0, 0],
18+
[0, 0, 0, 0, 0, 1, 0, 0],
19+
[0, 0, 0, 0, 0, 0, 1, 0],
20+
[0, 0, 0, 0, 0, 0, 0, 1],
21+
]
22+
)
23+
24+
25+
def test_grover_integration():
26+
num_qubits = 3
27+
optimal_iterations = optimal_num_iterations(num_solutions=1, num_qubits=num_qubits)
28+
grover_circuit = get_grover_circuit(num_qubits, U_w, optimal_iterations)
29+
grover_circuit.measure_all(add_bits=False)
30+
statevectorsampler = StatevectorSampler()
31+
result = statevectorsampler.run([grover_circuit]).result()
32+
_, answer = get_grover_answer(result)
33+
assert answer == "011", f"Expected '011', got {answer}"
34+
35+
with pytest.raises(ValueError): # check wrong number of qubits error
36+
get_grover_circuit(num_qubits=4, U_w=U_w, iterations=optimal_iterations)
37+
38+
with pytest.raises(TypeError): # check wrong result datatype error
39+
get_grover_answer(result[0])

tests/test_utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import jax.numpy as jnp
2+
import pytest
23
from scipy.linalg import null_space
34
from scipy.special import factorial2
45

56
from isotropic.utils.bisection import get_theta
67
from isotropic.utils.distribution import (
78
double_factorial_jax,
89
double_factorial_ratio_jax,
10+
double_factorial_ratio_scipy,
911
normal_integrand,
1012
)
1113
from isotropic.utils.linalg import jax_null_space
@@ -99,6 +101,14 @@ def test_double_factorial_ratio_jax():
99101
f"Expected {ratio_expected}, got {ratio_received}"
100102
)
101103

104+
with pytest.raises(ValueError): # check for error on inputs not close enough
105+
_ = double_factorial_ratio_jax(300, 290)
106+
107+
108+
def test_double_factorial_ratio_scipy():
109+
with pytest.raises(ValueError):
110+
_ = double_factorial_ratio_scipy(302, 301)
111+
102112

103113
def test_normal_integrand():
104114
theta = jnp.pi / 4 # 45 degrees

0 commit comments

Comments
 (0)