Skip to content

Commit

Permalink
fix test_sampler_v2
Browse files Browse the repository at this point in the history
  • Loading branch information
doichanj committed Aug 9, 2024
1 parent b60220f commit 9d5c4b7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
2 changes: 1 addition & 1 deletion test/terra/backends/aer_simulator/test_truncate.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def test_truncate_default(self):
result = backend.run(circuit, shots=1).result()
metadata = result.results[0].metadata
self.assertEqual(metadata["num_qubits"], 2)
self.assertEqual(metadata["active_input_qubits"], [0, 1])
self.assertEqual(len(metadata["active_input_qubits"]), 2)

def test_truncate_non_measured_qubits(self):
"""Test truncation of non-measured uncoupled qubits."""
Expand Down
50 changes: 32 additions & 18 deletions test/terra/primitives/test_sampler_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ def setUp(self):
self._shots = 10000
self._seed = 123
self._options = {"default_shots": self._shots, "seed": self._seed}
self._pm = generate_preset_pass_manager(optimization_level=0, backend=AerSimulator())

self._cases = []
hadamard = QuantumCircuit(1, 1, name="Hadamard")
Expand Down Expand Up @@ -82,10 +81,10 @@ def _assert_allclose(self, bitarray: BitArray, target: NDArray | BitArray, rtol=

def test_sampler_run(self):
"""Test run()."""
pm = self._pm

with self.subTest("single"):
bell, _, target = self._cases[1]
pm = generate_preset_pass_manager(optimization_level=0, backend=AerSimulator())
bell = pm.run(bell)
sampler = SamplerV2(**self._options)
job = sampler.run([bell], shots=self._shots)
Expand All @@ -102,6 +101,7 @@ def test_sampler_run(self):
with self.subTest("single with param"):
pqc, param_vals, target = self._cases[2]
sampler = SamplerV2(**self._options)
pm = generate_preset_pass_manager(optimization_level=0, backend=AerSimulator())
pqc = pm.run(pqc)
params = (param.name for param in pqc.parameters)
job = sampler.run([(pqc, {params: param_vals})], shots=self._shots)
Expand All @@ -118,6 +118,7 @@ def test_sampler_run(self):
with self.subTest("multiple"):
pqc, param_vals, target = self._cases[2]
sampler = SamplerV2(**self._options)
pm = generate_preset_pass_manager(optimization_level=0, backend=AerSimulator())
pqc = pm.run(pqc)
params = (param.name for param in pqc.parameters)
job = sampler.run(
Expand All @@ -137,7 +138,8 @@ def test_sampler_run_multiple_times(self):
"""Test run() returns the same results if the same input is given."""
bell, _, _ = self._cases[1]
sampler = SamplerV2(**self._options)
bell = self._pm.run(bell)
pm = generate_preset_pass_manager(optimization_level=0, backend=AerSimulator())
bell = pm.run(bell)
result1 = sampler.run([bell], shots=self._shots).result()
meas1 = result1[0].data.meas
result2 = sampler.run([bell], shots=self._shots).result()
Expand All @@ -148,7 +150,8 @@ def test_sample_run_multiple_circuits(self):
"""Test run() with multiple circuits."""
bell, _, target = self._cases[1]
sampler = SamplerV2(**self._options)
bell = self._pm.run(bell)
pm = generate_preset_pass_manager(optimization_level=0, backend=AerSimulator())
bell = pm.run(bell)
result = sampler.run([bell, bell, bell], shots=self._shots).result()
self.assertEqual(len(result), 3)
self._assert_allclose(result[0].data.meas, np.array(target))
Expand All @@ -160,7 +163,8 @@ def test_sampler_run_with_parameterized_circuits(self):
pqc1, param1, target1 = self._cases[4]
pqc2, param2, target2 = self._cases[5]
pqc3, param3, target3 = self._cases[6]
pqc1, pqc2, pqc3 = self._pm.run([pqc1, pqc2, pqc3])
pm = generate_preset_pass_manager(optimization_level=0, backend=AerSimulator())
pqc1, pqc2, pqc3 = pm.run([pqc1, pqc2, pqc3])

sampler = SamplerV2(**self._options)
result = sampler.run(
Expand All @@ -178,7 +182,8 @@ def test_run_1qubit(self):
qc2 = QuantumCircuit(1)
qc2.x(0)
qc2.measure_all()
qc, qc2 = self._pm.run([qc, qc2])
pm = generate_preset_pass_manager(optimization_level=0, backend=AerSimulator())
qc, qc2 = pm.run([qc, qc2])

sampler = SamplerV2(**self._options)
result = sampler.run([qc, qc2], shots=self._shots).result()
Expand All @@ -199,7 +204,8 @@ def test_run_2qubit(self):
qc3 = QuantumCircuit(2)
qc3.x([0, 1])
qc3.measure_all()
qc0, qc1, qc2, qc3 = self._pm.run([qc0, qc1, qc2, qc3])
pm = generate_preset_pass_manager(optimization_level=0, backend=AerSimulator())
qc0, qc1, qc2, qc3 = pm.run([qc0, qc1, qc2, qc3])

sampler = SamplerV2(**self._options)
result = sampler.run([qc0, qc1, qc2, qc3], shots=self._shots).result()
Expand All @@ -210,7 +216,7 @@ def test_run_2qubit(self):
def test_run_single_circuit(self):
"""Test for single circuit case."""
sampler = SamplerV2(**self._options)
pm = self._pm
pm = generate_preset_pass_manager(optimization_level=0, backend=AerSimulator())

with self.subTest("No parameter"):
circuit, _, target = self._cases[1]
Expand Down Expand Up @@ -276,7 +282,8 @@ def test_run_reverse_meas_order(self):
qc.measure(0, 2)
qc.measure(1, 1)
qc.measure(2, 0)
qc = self._pm.run(qc)
pm = generate_preset_pass_manager(optimization_level=0, backend=AerSimulator())
qc = pm.run(qc)

sampler = SamplerV2(**self._options)
sampler.options.seed_simulator = self._seed
Expand All @@ -295,7 +302,8 @@ def test_run_errors(self):
qc1.measure_all()
qc2 = RealAmplitudes(num_qubits=1, reps=1)
qc2.measure_all()
qc1, qc2 = self._pm.run([qc1, qc2])
pm = generate_preset_pass_manager(optimization_level=0, backend=AerSimulator())
qc1, qc2 = pm.run([qc1, qc2])

sampler = SamplerV2(**self._options)
with self.subTest("set parameter values to a non-parameterized circuit"):
Expand Down Expand Up @@ -344,7 +352,8 @@ def test_run_empty_parameter(self):
n = 5
qc = QuantumCircuit(n, n - 1)
qc.measure(range(n - 1), range(n - 1))
qc = self._pm.run(qc)
pm = generate_preset_pass_manager(optimization_level=0, backend=AerSimulator())
qc = pm.run(qc)
sampler = SamplerV2(**self._options)
with self.subTest("one circuit"):
result = sampler.run([qc], shots=self._shots).result()
Expand All @@ -361,7 +370,8 @@ def test_run_numpy_params(self):
"""Test for numpy array as parameter values"""
qc = RealAmplitudes(num_qubits=2, reps=2)
qc.measure_all()
qc = self._pm.run(qc)
pm = generate_preset_pass_manager(optimization_level=0, backend=AerSimulator())
qc = pm.run(qc)
k = 5
params_array = np.linspace(0, 1, k * qc.num_parameters).reshape((k, qc.num_parameters))
params_list = params_array.tolist()
Expand All @@ -388,7 +398,8 @@ def test_run_numpy_params(self):
def test_run_with_shots_option(self):
"""test with shots option."""
bell, _, _ = self._cases[1]
bell = self._pm.run(bell)
pm = generate_preset_pass_manager(optimization_level=0, backend=AerSimulator())
bell = pm.run(bell)
shots = 100

with self.subTest("run arg"):
Expand Down Expand Up @@ -443,7 +454,8 @@ def test_run_shots_result_size(self):
qc = QuantumCircuit(n)
qc.h(range(n))
qc.measure_all()
qc = self._pm.run(qc)
pm = generate_preset_pass_manager(optimization_level=0, backend=AerSimulator())
qc = pm.run(qc)
sampler = SamplerV2(**self._options)
result = sampler.run([qc], shots=self._shots).result()
self.assertEqual(len(result), 1)
Expand All @@ -453,15 +465,16 @@ def test_run_shots_result_size(self):
def test_primitive_job_status_done(self):
"""test primitive job's status"""
bell, _, _ = self._cases[1]
bell = self._pm.run(bell)
pm = generate_preset_pass_manager(optimization_level=0, backend=AerSimulator())
bell = pm.run(bell)
sampler = SamplerV2(**self._options)
job = sampler.run([bell], shots=self._shots)
_ = job.result()
self.assertEqual(job.status(), JobStatus.DONE)

def test_circuit_with_unitary(self):
"""Test for circuit with unitary gate."""
pm = self._pm
pm = generate_preset_pass_manager(optimization_level=0, backend=AerSimulator())

with self.subTest("identity"):
gate = UnitaryGate(np.eye(2))
Expand Down Expand Up @@ -491,7 +504,7 @@ def test_circuit_with_unitary(self):

def test_circuit_with_multiple_cregs(self):
"""Test for circuit with multiple classical registers."""
pm = self._pm
pm = generate_preset_pass_manager(optimization_level=0, backend=AerSimulator())
cases = []

# case 1
Expand Down Expand Up @@ -632,7 +645,8 @@ def test_empty_creg(self):
def test_diff_shots(self):
"""Test of pubs with different shots"""
bell, _, target = self._cases[1]
bell = self._pm.run(bell)
pm = generate_preset_pass_manager(optimization_level=0, backend=AerSimulator())
bell = pm.run(bell)
sampler = SamplerV2(**self._options)
shots2 = self._shots + 2
target2 = {k: v + 1 for k, v in target.items()}
Expand Down

0 comments on commit 9d5c4b7

Please sign in to comment.