Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid flaky test execution in z_phase_calibration_test #6928

Merged
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
22 changes: 13 additions & 9 deletions cirq-core/cirq/experiments/z_phase_calibration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
from cirq.experiments.xeb_fitting import XEBPhasedFSimCharacterizationOptions

_ANGLES = ['theta', 'phi', 'chi', 'zeta', 'gamma']
# fix random generator seed to ensure reproducibility and faster convergence
_SEED = 276154030


def _create_tests(n, seed, with_options: bool = False):
rng = np.random.default_rng(seed)
def _create_tests(n, with_options: bool = False):
rng = np.random.default_rng(_SEED)
angles = (rng.random((n, 5)) * 2 - 1) * np.pi
# Add errors to the last 3 angles (chi, zeta, gamma).
# The errors are in the union (-2, -1) U (1, 2).
Expand Down Expand Up @@ -86,8 +88,7 @@ def _core_iterator(


@pytest.mark.parametrize(
['angles', 'error', 'characterization_flags'],
_create_tests(n=10, seed=32432432, with_options=True),
['angles', 'error', 'characterization_flags'], _create_tests(n=10, with_options=True)
)
def test_calibrate_z_phases(angles, error, characterization_flags):

Expand All @@ -98,7 +99,7 @@ def test_calibrate_z_phases(angles, error, characterization_flags):
**{f'{n}_default': t for n, t in zip(_ANGLES, angles)}, **characterization_flags
)

sampler = _TestSimulator(original_gate, actual_gate, seed=0)
sampler = _TestSimulator(original_gate, actual_gate, seed=_SEED)
qubits = cirq.q(0, 0), cirq.q(0, 1)
calibrated_gate = calibrate_z_phases(
sampler,
Expand All @@ -109,6 +110,7 @@ def test_calibrate_z_phases(angles, error, characterization_flags):
n_combinations=10,
n_circuits=10,
cycle_depths=range(3, 10),
random_state=_SEED,
)[qubits]

initial_unitary = cirq.unitary(original_gate)
Expand All @@ -126,13 +128,13 @@ def test_calibrate_z_phases(angles, error, characterization_flags):
assert new_dist < original_dist or new_dist < 1e-6


@pytest.mark.parametrize(['angles', 'error'], _create_tests(n=3, seed=32432432))
@pytest.mark.parametrize(['angles', 'error'], _create_tests(n=3))
def test_calibrate_z_phases_no_options(angles, error):

original_gate = cirq.PhasedFSimGate(**{k: v for k, v in zip(_ANGLES, angles)})
actual_gate = cirq.PhasedFSimGate(**{k: v + e for k, v, e in zip(_ANGLES, angles, error)})

sampler = _TestSimulator(original_gate, actual_gate, seed=0)
sampler = _TestSimulator(original_gate, actual_gate, seed=_SEED)
qubits = cirq.q(0, 0), cirq.q(0, 1)
calibrated_gate = calibrate_z_phases(
sampler,
Expand All @@ -143,6 +145,7 @@ def test_calibrate_z_phases_no_options(angles, error):
n_combinations=10,
n_circuits=10,
cycle_depths=range(3, 10),
random_state=_SEED,
)[qubits]

initial_unitary = cirq.unitary(original_gate)
Expand All @@ -160,13 +163,13 @@ def test_calibrate_z_phases_no_options(angles, error):
assert new_dist < original_dist or new_dist < 1e-6


@pytest.mark.parametrize(['angles', 'error'], _create_tests(n=3, seed=32432432))
@pytest.mark.parametrize(['angles', 'error'], _create_tests(n=3))
def test_calibrate_z_phases_workflow_no_options(angles, error):

original_gate = cirq.PhasedFSimGate(**{k: v for k, v in zip(_ANGLES, angles)})
actual_gate = cirq.PhasedFSimGate(**{k: v + e for k, v, e in zip(_ANGLES, angles, error)})

sampler = _TestSimulator(original_gate, actual_gate, seed=0)
sampler = _TestSimulator(original_gate, actual_gate, seed=_SEED)
qubits = cirq.q(0, 0), cirq.q(0, 1)
result, _ = z_phase_calibration_workflow(
sampler,
Expand All @@ -177,6 +180,7 @@ def test_calibrate_z_phases_workflow_no_options(angles, error):
n_combinations=1,
n_circuits=1,
cycle_depths=(1, 2),
random_state=_SEED,
)

for params in result.final_params.values():
Expand Down