Skip to content

Commit 08a15c8

Browse files
authored
Optimize pauli_string_measurement_with_readout_mitigation_test (#7740)
Decrease count-like arguments of `measure_pauli_strings` from 1000 to 300 and increase the simulated readout errors so they can be estimated with fewer repetitions. Related to b/454743228
1 parent 4cbe88d commit 08a15c8

File tree

1 file changed

+40
-45
lines changed

1 file changed

+40
-45
lines changed

cirq-core/cirq/contrib/paulistring/pauli_string_measurement_with_readout_mitigation_test.py

Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def test_pauli_string_measurement_errors_no_noise(use_sweep: bool) -> None:
117117
circuits_to_pauli[circuit] = [_generate_random_pauli_string(qubits) for _ in range(3)]
118118

119119
circuits_with_pauli_expectations = measure_pauli_strings(
120-
circuits_to_pauli, sampler, 1000, 1000, 1000, 1000, use_sweep
120+
circuits_to_pauli, sampler, 300, 300, 300, 1234, use_sweep
121121
)
122122

123123
for circuit_with_pauli_expectations in circuits_with_pauli_expectations:
@@ -172,7 +172,7 @@ def test_group_pauli_string_measurement_errors_no_noise_with_coefficient(use_swe
172172
circuits_to_pauli[circuit].append([cirq.PauliString({q: cirq.X for q in qubits})])
173173

174174
circuits_with_pauli_expectations = measure_pauli_strings(
175-
circuits_to_pauli, sampler, 1000, 1000, 1000, 500, use_sweep
175+
circuits_to_pauli, sampler, 300, 300, 300, 1234, use_sweep
176176
)
177177

178178
for circuit_with_pauli_expectations in circuits_with_pauli_expectations:
@@ -214,14 +214,14 @@ def test_pauli_string_measurement_errors_with_noise(use_sweep: bool) -> None:
214214
based on the Pauli string"""
215215
qubits = cirq.LineQubit.range(7)
216216
circuit = cirq.FrozenCircuit(_create_ghz(7, qubits))
217-
sampler = NoisySingleQubitReadoutSampler(p0=0.01, p1=0.005, seed=1234)
217+
sampler = NoisySingleQubitReadoutSampler(p0=0.01, p1=0.05, seed=1234)
218218
simulator = cirq.Simulator()
219219

220220
circuits_to_pauli: dict[cirq.FrozenCircuit, list[cirq.PauliString]] = {}
221221
circuits_to_pauli[circuit] = [_generate_random_pauli_string(qubits) for _ in range(3)]
222222

223223
circuits_with_pauli_expectations = measure_pauli_strings(
224-
circuits_to_pauli, sampler, 1000, 1000, 1000, np.random.default_rng(), use_sweep
224+
circuits_to_pauli, sampler, 300, 300, 300, np.random.default_rng(), use_sweep
225225
)
226226

227227
for circuit_with_pauli_expectations in circuits_with_pauli_expectations:
@@ -253,7 +253,7 @@ def test_pauli_string_measurement_errors_with_noise(use_sweep: bool) -> None:
253253
for (
254254
error
255255
) in pauli_string_measurement_results.calibration_result.one_state_errors.values():
256-
assert 0.0045 < error < 0.0055
256+
assert 0.045 < error < 0.055
257257

258258

259259
@pytest.mark.parametrize("use_sweep", [True, False])
@@ -262,7 +262,7 @@ def test_group_pauli_string_measurement_errors_with_noise(use_sweep: bool) -> No
262262
based on the group Pauli strings"""
263263
qubits = cirq.LineQubit.range(7)
264264
circuit = cirq.FrozenCircuit(_create_ghz(7, qubits))
265-
sampler = NoisySingleQubitReadoutSampler(p0=0.01, p1=0.005, seed=1234)
265+
sampler = NoisySingleQubitReadoutSampler(p0=0.01, p1=0.05, seed=1234)
266266
simulator = cirq.Simulator()
267267

268268
circuits_to_pauli: dict[cirq.FrozenCircuit, list[list[cirq.PauliString]]] = {}
@@ -273,7 +273,7 @@ def test_group_pauli_string_measurement_errors_with_noise(use_sweep: bool) -> No
273273
]
274274

275275
circuits_with_pauli_expectations = measure_pauli_strings(
276-
circuits_to_pauli, sampler, 1000, 1000, 1000, np.random.default_rng(), use_sweep
276+
circuits_to_pauli, sampler, 300, 300, 300, np.random.default_rng(), use_sweep
277277
)
278278

279279
for circuit_with_pauli_expectations in circuits_with_pauli_expectations:
@@ -305,7 +305,7 @@ def test_group_pauli_string_measurement_errors_with_noise(use_sweep: bool) -> No
305305
for (
306306
error
307307
) in pauli_string_measurement_results.calibration_result.one_state_errors.values():
308-
assert 0.0045 < error < 0.0055
308+
assert 0.045 < error < 0.055
309309

310310

311311
@pytest.mark.parametrize("use_sweep", [True, False])
@@ -331,11 +331,11 @@ def test_many_circuits_input_measurement_with_noise(use_sweep: bool) -> None:
331331
circuits_to_pauli[circuit_2] = [_generate_random_pauli_string(qubits_2) for _ in range(3)]
332332
circuits_to_pauli[circuit_3] = [_generate_random_pauli_string(qubits_3) for _ in range(3)]
333333

334-
sampler = NoisySingleQubitReadoutSampler(p0=0.003, p1=0.005, seed=1234)
334+
sampler = NoisySingleQubitReadoutSampler(p0=0.03, p1=0.05, seed=1234)
335335
simulator = cirq.Simulator()
336336

337337
circuits_with_pauli_expectations = measure_pauli_strings(
338-
circuits_to_pauli, sampler, 1000, 1000, 1000, np.random.default_rng(), use_sweep
338+
circuits_to_pauli, sampler, 300, 300, 300, np.random.default_rng(), use_sweep
339339
)
340340

341341
for circuit_with_pauli_expectations in circuits_with_pauli_expectations:
@@ -361,11 +361,11 @@ def test_many_circuits_input_measurement_with_noise(use_sweep: bool) -> None:
361361
for (
362362
error
363363
) in pauli_string_measurement_results.calibration_result.zero_state_errors.values():
364-
assert 0.0025 < error < 0.0035
364+
assert 0.025 < error < 0.035
365365
for (
366366
error
367367
) in pauli_string_measurement_results.calibration_result.one_state_errors.values():
368-
assert 0.0045 < error < 0.0055
368+
assert 0.045 < error < 0.055
369369

370370

371371
@pytest.mark.parametrize("use_sweep", [True, False])
@@ -383,7 +383,7 @@ def test_allow_group_pauli_measurement_without_readout_mitigation(use_sweep: boo
383383
]
384384

385385
circuits_with_pauli_expectations = measure_pauli_strings(
386-
circuits_to_pauli, sampler, 1000, 1000, 0, np.random.default_rng(), use_sweep
386+
circuits_to_pauli, sampler, 300, 300, 0, np.random.default_rng(), use_sweep
387387
)
388388

389389
for circuit_with_pauli_expectations in circuits_with_pauli_expectations:
@@ -427,15 +427,15 @@ def test_many_circuits_with_coefficient(
427427
circuits_to_pauli[circuit_2] = [_generate_random_pauli_string(qubits_2, True) for _ in range(3)]
428428
circuits_to_pauli[circuit_3] = [_generate_random_pauli_string(qubits_3, True) for _ in range(3)]
429429

430-
sampler = NoisySingleQubitReadoutSampler(p0=0.003, p1=0.005, seed=1234)
430+
sampler = NoisySingleQubitReadoutSampler(p0=0.03, p1=0.05, seed=1234)
431431
simulator = cirq.Simulator()
432432

433433
circuits_with_pauli_expectations = measure_pauli_strings(
434434
circuits_to_pauli,
435435
sampler,
436-
1000,
437-
1000,
438-
1000,
436+
300,
437+
300,
438+
300,
439439
np.random.default_rng(),
440440
use_sweep,
441441
insert_strategy,
@@ -464,11 +464,11 @@ def test_many_circuits_with_coefficient(
464464
for (
465465
error
466466
) in pauli_string_measurement_results.calibration_result.zero_state_errors.values():
467-
assert 0.0025 < error < 0.0035
467+
assert 0.025 < error < 0.035
468468
for (
469469
error
470470
) in pauli_string_measurement_results.calibration_result.one_state_errors.values():
471-
assert 0.0045 < error < 0.0055
471+
assert 0.045 < error < 0.055
472472

473473

474474
@pytest.mark.parametrize("use_sweep", [True, False])
@@ -506,11 +506,11 @@ def test_many_group_pauli_in_circuits_with_coefficient(use_sweep: bool) -> None:
506506
)
507507
]
508508

509-
sampler = NoisySingleQubitReadoutSampler(p0=0.003, p1=0.005, seed=1234)
509+
sampler = NoisySingleQubitReadoutSampler(p0=0.03, p1=0.05, seed=1234)
510510
simulator = cirq.Simulator()
511511

512512
circuits_with_pauli_expectations = measure_pauli_strings(
513-
circuits_to_pauli, sampler, 1000, 1000, 1000, np.random.default_rng(), use_sweep
513+
circuits_to_pauli, sampler, 300, 300, 300, np.random.default_rng(), use_sweep
514514
)
515515

516516
for circuit_with_pauli_expectations in circuits_with_pauli_expectations:
@@ -536,11 +536,11 @@ def test_many_group_pauli_in_circuits_with_coefficient(use_sweep: bool) -> None:
536536
for (
537537
error
538538
) in pauli_string_measurement_results.calibration_result.zero_state_errors.values():
539-
assert 0.0025 < error < 0.035
539+
assert 0.025 < error < 0.035
540540
for (
541541
error
542542
) in pauli_string_measurement_results.calibration_result.one_state_errors.values():
543-
assert 0.0045 < error < 0.0055
543+
assert 0.045 < error < 0.055
544544

545545

546546
def test_coefficient_not_real_number() -> None:
@@ -563,7 +563,7 @@ def test_coefficient_not_real_number() -> None:
563563
"non-Hermitian PauliString. Coefficient must be real.",
564564
):
565565
measure_pauli_strings(
566-
circuits_to_pauli, cirq.Simulator(), 1000, 1000, 1000, np.random.default_rng()
566+
circuits_to_pauli, cirq.Simulator(), 300, 300, 300, np.random.default_rng()
567567
)
568568

569569

@@ -572,12 +572,7 @@ def test_empty_input_circuits_to_pauli_mapping() -> None:
572572

573573
with pytest.raises(ValueError, match="Input circuits must not be empty."):
574574
measure_pauli_strings(
575-
[], # type: ignore[arg-type]
576-
cirq.Simulator(),
577-
1000,
578-
1000,
579-
1000,
580-
np.random.default_rng(),
575+
[], cirq.Simulator(), 300, 300, 300, np.random.default_rng() # type: ignore[arg-type]
581576
)
582577

583578

@@ -593,9 +588,9 @@ def test_invalid_input_circuit_type() -> None:
593588
measure_pauli_strings(
594589
qubits_to_pauli, # type: ignore[arg-type]
595590
cirq.Simulator(),
596-
1000,
597-
1000,
598-
1000,
591+
300,
592+
300,
593+
300,
599594
np.random.default_rng(),
600595
)
601596

@@ -626,9 +621,9 @@ def test_invalid_input_pauli_string_type() -> None:
626621
measure_pauli_strings(
627622
circuits_to_pauli, # type: ignore[arg-type]
628623
cirq.Simulator(),
629-
1000,
630-
1000,
631-
1000,
624+
300,
625+
300,
626+
300,
632627
np.random.default_rng(),
633628
)
634629

@@ -661,7 +656,7 @@ def test_all_pauli_strings_are_pauli_i() -> None:
661656
"valid input Pauli strings.",
662657
):
663658
measure_pauli_strings(
664-
circuits_to_pauli, cirq.Simulator(), 1000, 1000, 1000, np.random.default_rng()
659+
circuits_to_pauli, cirq.Simulator(), 300, 300, 300, np.random.default_rng()
665660
)
666661

667662

@@ -675,7 +670,7 @@ def test_zero_pauli_repetitions() -> None:
675670
circuits_to_pauli[circuit] = [cirq.PauliString({q: cirq.X for q in qubits})]
676671
with pytest.raises(ValueError, match="Must provide positive pauli_repetitions."):
677672
measure_pauli_strings(
678-
circuits_to_pauli, cirq.Simulator(), 0, 1000, 1000, np.random.default_rng()
673+
circuits_to_pauli, cirq.Simulator(), 0, 300, 300, np.random.default_rng()
679674
)
680675

681676

@@ -689,7 +684,7 @@ def test_negative_num_random_bitstrings() -> None:
689684
circuits_to_pauli[circuit] = [cirq.PauliString({q: cirq.X for q in qubits})]
690685
with pytest.raises(ValueError, match="Must provide zero or more num_random_bitstrings."):
691686
measure_pauli_strings(
692-
circuits_to_pauli, cirq.Simulator(), 1000, 1000, -1, np.random.default_rng()
687+
circuits_to_pauli, cirq.Simulator(), 300, 300, -1, np.random.default_rng()
693688
)
694689

695690

@@ -705,7 +700,7 @@ def test_zero_readout_repetitions() -> None:
705700
ValueError, match="Must provide positive readout_repetitions for readout" + " calibration."
706701
):
707702
measure_pauli_strings(
708-
circuits_to_pauli, cirq.Simulator(), 1000, 0, 1000, np.random.default_rng()
703+
circuits_to_pauli, cirq.Simulator(), 300, 0, 300, np.random.default_rng()
709704
)
710705

711706

@@ -719,7 +714,7 @@ def test_rng_type_mismatch() -> None:
719714
circuits_to_pauli[circuit] = [cirq.PauliString({q: cirq.X for q in qubits})]
720715
with pytest.raises(ValueError, match="Must provide a numpy random generator or a seed"):
721716
measure_pauli_strings(
722-
circuits_to_pauli, cirq.Simulator(), 1000, 1000, 1000, "test" # type: ignore[arg-type]
717+
circuits_to_pauli, cirq.Simulator(), 300, 300, 300, "test" # type: ignore[arg-type]
723718
)
724719

725720

@@ -737,7 +732,7 @@ def test_pauli_type_mismatch() -> None:
737732
" ops.PauliStrings. Got <class 'int'> instead.",
738733
):
739734
measure_pauli_strings(
740-
circuits_to_pauli, cirq.Simulator(), 1000, 1000, 1000, 1 # type: ignore[arg-type]
735+
circuits_to_pauli, cirq.Simulator(), 300, 300, 300, 1234 # type: ignore[arg-type]
741736
)
742737

743738

@@ -756,7 +751,7 @@ def test_group_paulis_are_not_qwc() -> None:
756751
ValueError, match="The group of Pauli strings are not Qubit-Wise Commuting with each other."
757752
):
758753
measure_pauli_strings(
759-
circuits_to_pauli, cirq.Simulator(), 1000, 1000, 1000, np.random.default_rng()
754+
circuits_to_pauli, cirq.Simulator(), 300, 300, 300, np.random.default_rng()
760755
)
761756

762757

@@ -770,7 +765,7 @@ def test_empty_group_paulis_not_allowed() -> None:
770765
circuits_to_pauli[circuit] = [[]] # type: ignore
771766
with pytest.raises(ValueError, match="Empty group of Pauli strings is not allowed"):
772767
measure_pauli_strings(
773-
circuits_to_pauli, cirq.Simulator(), 1000, 1000, 1000, np.random.default_rng()
768+
circuits_to_pauli, cirq.Simulator(), 300, 300, 300, np.random.default_rng()
774769
)
775770

776771

@@ -806,5 +801,5 @@ def test_group_paulis_type_mismatch() -> None:
806801
"but found <class 'cirq.ops.pauli_string.PauliString'>.",
807802
):
808803
measure_pauli_strings(
809-
circuits_to_pauli, cirq.Simulator(), 1000, 1000, 1000, np.random.default_rng()
804+
circuits_to_pauli, cirq.Simulator(), 300, 300, 300, np.random.default_rng()
810805
)

0 commit comments

Comments
 (0)