unitaryfund / mitiq

Mitiq is an open source toolkit for implementing error mitigation techniques on most current intermediate-scale quantum computers.
https://mitiq.readthedocs.io
GNU General Public License v3.0
358 stars 157 forks source link

Flaky tests with `cirq.PauliSumCollector` #721

Closed rmlarose closed 2 years ago

rmlarose commented 3 years ago

Any test like test_execute_with_shots which uses cirq.PauliSumCollector occasionally fails in CI. Example error message below.

``` ___________________________ test_execute_with_shots ____________________________ [gw1] linux -- Python 3.7.10 /opt/hostedtoolcache/Python/3.7.10/x64/bin/python def test_execute_with_shots(): """Tests if executor function for Cirq returns a proper expectation value when considering finite number of samples (aka shots).""" shots = 1000 observable = cirq.PauliString( cirq.ops.Z.on(cirq.LineQubit(0)), cirq.ops.Z.on(cirq.LineQubit(1)) ) qc = cirq.Circuit() qc += [ cirq.X(cirq.LineQubit(0)), cirq.CNOT(cirq.LineQubit(0), cirq.LineQubit(1)), ] > observable_exp_value = execute_with_shots(qc, observable, shots) mitiq/mitiq_cirq/tests/test_cirq_utils.py:62: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ mitiq/mitiq_cirq/cirq_utils.py:54: in execute_with_shots psum.collect(sampler=cirq.Simulator()) /opt/hostedtoolcache/Python/3.7.10/x64/lib/python3.7/site-packages/cirq/work/collector.py:133: in collect return asyncio.get_event_loop().run_until_complete( _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ self = def get_event_loop(self): """Get the event loop for the current context. Returns an instance of EventLoop or raises an exception. """ if (self._local._loop is None and not self._local._set_called and isinstance(threading.current_thread(), threading._MainThread)): self.set_event_loop(self.new_event_loop()) if self._local._loop is None: raise RuntimeError('There is no current event loop in thread %r.' > % threading.current_thread().name) E RuntimeError: There is no current event loop in thread 'Dummy-2'. /opt/hostedtoolcache/Python/3.7.10/x64/lib/python3.7/asyncio/events.py:644: RuntimeError ________________ test_execute_with_shots_and_depolarizing_noise ________________ [gw1] linux -- Python 3.7.10 /opt/hostedtoolcache/Python/3.7.10/x64/bin/python def test_execute_with_shots_and_depolarizing_noise(): """Tests if executor function for Cirq returns a proper expectation value when used for noisy depoalrizing simulation and a finite number of samples (aka shots).""" shots = 200 qc = cirq.Circuit() for _ in range(4): qc += cirq.X(cirq.LineQubit(0)) qc += cirq.measure(cirq.LineQubit(0)) # introduce noise in the circuit qc = qc.with_noise(cirq.depolarize(p=0.02)) observable = cirq.PauliString(cirq.ops.Z.on(cirq.LineQubit(0))) p_d = 0.01 # depolarizing noise strength observable_exp_value = execute_with_shots_and_depolarizing_noise( > qc, observable, p_d, shots ) mitiq/mitiq_cirq/tests/test_cirq_utils.py:115: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ mitiq/mitiq_cirq/cirq_utils.py:99: in execute_with_shots_and_depolarizing_noise psum.collect(sampler=cirq.DensityMatrixSimulator()) /opt/hostedtoolcache/Python/3.7.10/x64/lib/python3.7/site-packages/cirq/work/collector.py:133: in collect return asyncio.get_event_loop().run_until_complete( _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ self = def get_event_loop(self): """Get the event loop for the current context. Returns an instance of EventLoop or raises an exception. """ if (self._local._loop is None and not self._local._set_called and isinstance(threading.current_thread(), threading._MainThread)): self.set_event_loop(self.new_event_loop()) if self._local._loop is None: raise RuntimeError('There is no current event loop in thread %r.' > % threading.current_thread().name) E RuntimeError: There is no current event loop in thread 'Dummy-2'. /opt/hostedtoolcache/Python/3.7.10/x64/lib/python3.7/asyncio/events.py:644: RuntimeError ```
rmlarose commented 3 years ago

The status of this may change after moving back to Cirq 0.10 in #717. Let's see if any tests continue to fail.

rmlarose commented 3 years ago

The status of this may change after moving back to Cirq 0.10 in #717. Let's see if any tests continue to fail.

The change to Cirq 0.10 does not seem to have affected this, as it just popped up in CI again: https://github.com/unitaryfund/mitiq/runs/2889110440?check_suite_focus=true#step:6:2057

andreamari commented 3 years ago

This error doesn't appear in CI anymore since a long time. Probably it was due to some subtle problem in Cirq (or in Python?) that seems to be solved. Therefore I am closing this, but please reopen if someone disagrees.

rmlarose commented 3 years ago

Just saw this the other day actually, still around: https://github.com/unitaryfund/mitiq/runs/3502306803?check_suite_focus=true

andreamari commented 3 years ago

ops, sorry

github-actions[bot] commented 2 years ago

This issue had no activity for 2 months, and will be closed in one week unless there is new activity. Cheers!

github-actions[bot] commented 2 years ago

This issue had no activity for 2 months, and will be closed in one week unless there is new activity. Cheers!

andreamari commented 2 years ago

Based on last Friday meeting, the plan for this issue is to:

The reason for this strategy is that these functions are not essential anymore, since we can now use a mitiq.Observable.