Skip to content
Merged
Changes from all 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
47 changes: 18 additions & 29 deletions test/python/primitives/test_statevector_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ def _assert_allclose(self, bitarray: BitArray, target: NDArray | BitArray, rtol=

def test_sampler_run(self):
"""Test run()."""
bell, _, target = self._cases[1]

with self.subTest("single"):
bell, _, target = self._cases[1]
sampler = StatevectorSampler(seed=self._seed)
job = sampler.run([bell], shots=self._shots)
result = job.result()
Expand All @@ -93,8 +93,10 @@ def test_sampler_run(self):
self._assert_allclose(result[0].data.meas, np.array(target))

with self.subTest("single with param"):
pqc, param_vals, target = self._cases[2]
sampler = StatevectorSampler(seed=self._seed)
job = sampler.run([(bell, ())], shots=self._shots)
params = (param.name for param in pqc.parameters)
job = sampler.run([(pqc, {params: param_vals})], shots=self._shots)
result = job.result()
self.assertIsInstance(result, PrimitiveResult)
self.assertIsInstance(result.metadata, dict)
Expand All @@ -104,21 +106,13 @@ def test_sampler_run(self):
self.assertIsInstance(result[0].data.meas, BitArray)
self._assert_allclose(result[0].data.meas, np.array(target))

with self.subTest("single array"):
sampler = StatevectorSampler(seed=self._seed)
job = sampler.run([(bell, [()])], shots=self._shots)
result = job.result()
self.assertIsInstance(result, PrimitiveResult)
self.assertIsInstance(result.metadata, dict)
self.assertEqual(len(result), 1)
self.assertIsInstance(result[0], PubResult)
self.assertIsInstance(result[0].data, DataBin)
self.assertIsInstance(result[0].data.meas, BitArray)
self._assert_allclose(result[0].data.meas, np.array([target]))

with self.subTest("multiple"):
pqc, param_vals, target = self._cases[2]
sampler = StatevectorSampler(seed=self._seed)
job = sampler.run([(bell, [(), (), ()])], shots=self._shots)
params = (param.name for param in pqc.parameters)
job = sampler.run(
[(pqc, {params: [param_vals, param_vals, param_vals]})], shots=self._shots
)
result = job.result()
self.assertIsInstance(result, PrimitiveResult)
self.assertIsInstance(result.metadata, dict)
Expand Down Expand Up @@ -206,14 +200,7 @@ def test_run_single_circuit(self):
circuit, _, target = self._cases[1]
param_target = [
(None, np.array(target)),
((), np.array(target)),
([], np.array(target)),
(np.array([]), np.array(target)),
(((),), np.array([target])),
(([],), np.array([target])),
([[]], np.array([target])),
([()], np.array([target])),
(np.array([[]]), np.array([target])),
({}, np.array(target)),
]
for param, target in param_target:
with self.subTest(f"{circuit.name} w/ {param}"):
Expand All @@ -228,12 +215,14 @@ def test_run_single_circuit(self):
circuit.ry(param, 0)
circuit.measure(0, 0)
param_target = [
([np.pi], np.array({1: self._shots})),
((np.pi,), np.array({1: self._shots})),
(np.array([np.pi]), np.array({1: self._shots})),
([[np.pi]], np.array([{1: self._shots}])),
(((np.pi,),), np.array([{1: self._shots}])),
(np.array([[np.pi]]), np.array([{1: self._shots}])),
({"x": np.pi}, np.array({1: self._shots})),
({param: np.pi}, np.array({1: self._shots})),
({"x": np.array(np.pi)}, np.array({1: self._shots})),
({param: np.array(np.pi)}, np.array({1: self._shots})),
({"x": [np.pi]}, np.array([{1: self._shots}])),
({param: [np.pi]}, np.array([{1: self._shots}])),
({"x": np.array([np.pi])}, np.array([{1: self._shots}])),
({param: np.array([np.pi])}, np.array([{1: self._shots}])),
]
for param, target in param_target:
with self.subTest(f"{circuit.name} w/ {param}"):
Expand Down