Skip to content

Commit

Permalink
Fix unit tests against qiskit/main (#1099)
Browse files Browse the repository at this point in the history
* Add measurements to sampler

* Remove observables from sampler run

---------

Co-authored-by: Kevin Tian <[email protected]>
  • Loading branch information
ElePT and kt474 authored Sep 21, 2023
1 parent b7fef82 commit 25e0b2b
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions test/unit/test_ibm_primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,12 @@ def test_default_error_levels(self):
simulator={"noise_model": "foo"},
)
inst = cls(session=session, options=options)
inst.run(self.qx, observables=self.obs)

if isinstance(inst, Estimator):
inst.run(self.qx, observables=self.obs)
else:
inst.run(self.qx)

if sys.version_info >= (3, 8):
inputs = session.run.call_args.kwargs["inputs"]
else:
Expand Down Expand Up @@ -730,11 +735,12 @@ def test_raise_faulty_qubits(self):
estimator = Estimator(session=session)

with self.assertRaises(ValueError) as err:
sampler.run(transpiled, skip_transpilation=True)
estimator.run(transpiled, observable, skip_transpilation=True)
self.assertIn(f"faulty qubit {faulty_qubit}", str(err.exception))

transpiled.measure_all()
with self.assertRaises(ValueError) as err:
estimator.run(transpiled, observable, skip_transpilation=True)
sampler.run(transpiled, skip_transpilation=True)
self.assertIn(f"faulty qubit {faulty_qubit}", str(err.exception))

def test_raise_faulty_qubits_many(self):
Expand All @@ -759,11 +765,14 @@ def test_raise_faulty_qubits_many(self):
estimator = Estimator(session=session)

with self.assertRaises(ValueError) as err:
sampler.run(transpiled, skip_transpilation=True)
estimator.run(transpiled, [observable, observable], skip_transpilation=True)
self.assertIn(f"faulty qubit {faulty_qubit}", str(err.exception))

for circ in transpiled:
circ.measure_all()

with self.assertRaises(ValueError) as err:
estimator.run(transpiled, [observable, observable], skip_transpilation=True)
sampler.run(transpiled, skip_transpilation=True)
self.assertIn(f"faulty qubit {faulty_qubit}", str(err.exception))

def test_raise_faulty_edge(self):
Expand All @@ -785,12 +794,13 @@ def test_raise_faulty_edge(self):
estimator = Estimator(session=session)

with self.assertRaises(ValueError) as err:
sampler.run(transpiled, skip_transpilation=True)
estimator.run(transpiled, observable, skip_transpilation=True)
self.assertIn("cx", str(err.exception))
self.assertIn(f"faulty edge {tuple(edge_qubits)}", str(err.exception))

transpiled.measure_all()
with self.assertRaises(ValueError) as err:
estimator.run(transpiled, observable, skip_transpilation=True)
sampler.run(transpiled, skip_transpilation=True)
self.assertIn("cx", str(err.exception))
self.assertIn(f"faulty edge {tuple(edge_qubits)}", str(err.exception))

Expand All @@ -813,11 +823,12 @@ def test_faulty_qubit_not_used(self):
estimator = Estimator(session=session)

with patch.object(Session, "run") as mock_run:
sampler.run(transpiled, skip_transpilation=True)
estimator.run(transpiled, observable, skip_transpilation=True)
mock_run.assert_called_once()

transpiled.measure_active()
with patch.object(Session, "run") as mock_run:
estimator.run(transpiled, observable, skip_transpilation=True)
sampler.run(transpiled, skip_transpilation=True)
mock_run.assert_called_once()

def test_faulty_edge_not_used(self):
Expand All @@ -841,11 +852,12 @@ def test_faulty_edge_not_used(self):
estimator = Estimator(session=session)

with patch.object(Session, "run") as mock_run:
sampler.run(transpiled, skip_transpilation=True)
estimator.run(transpiled, observable, skip_transpilation=True)
mock_run.assert_called_once()

transpiled.measure_all()
with patch.object(Session, "run") as mock_run:
estimator.run(transpiled, observable, skip_transpilation=True)
sampler.run(transpiled, skip_transpilation=True)
mock_run.assert_called_once()

def test_no_raise_skip_transpilation(self):
Expand All @@ -870,11 +882,12 @@ def test_no_raise_skip_transpilation(self):
estimator = Estimator(session=session)

with patch.object(Session, "run") as mock_run:
sampler.run(transpiled)
estimator.run(transpiled, observable)
mock_run.assert_called_once()

transpiled.measure_all()
with patch.object(Session, "run") as mock_run:
estimator.run(transpiled, observable)
sampler.run(transpiled)
mock_run.assert_called_once()

def _update_dict(self, dict1, dict2):
Expand Down

0 comments on commit 25e0b2b

Please sign in to comment.