Skip to content

Commit

Permalink
Default scaling method in modularized ZNE function (#2666)
Browse files Browse the repository at this point in the history
* add default option and copy-paste unit test

* mypy failure

* scaled_circuits default option assert

* nate's suggestion: change to_frontend

* separate test
  • Loading branch information
purva-thakre authored Feb 14, 2025
1 parent 4e1996a commit c1be865
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
25 changes: 21 additions & 4 deletions mitiq/zne/tests/test_zne.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,11 +584,11 @@ def qs_noisy_simulation(
"extrapolation_factory", [RichardsonFactory, LinearFactory]
)
@pytest.mark.parametrize(
"to_frontend",
"conversion_func",
[None, to_qiskit, to_braket, to_pennylane, to_pyquil, to_qibo],
)
def test_two_stage_zne(
noise_scaling_method, extrapolation_factory, to_frontend
noise_scaling_method, extrapolation_factory, conversion_func
):
qreg = cirq.LineQubit.range(2)
cirq_circuit = cirq.Circuit(
Expand All @@ -597,8 +597,8 @@ def test_two_stage_zne(
cirq.CNOT(*qreg),
cirq.H.on_each(qreg),
)
if to_frontend is not None:
frontend_circuit = to_frontend(cirq_circuit)
if conversion_func is not None:
frontend_circuit = conversion_func(cirq_circuit)
else:
frontend_circuit = cirq_circuit

Expand Down Expand Up @@ -630,3 +630,20 @@ def executor(circuit):
scale_noise=noise_scaling_method,
)
assert np.isclose(zne_res, two_stage_zne_res)


def test_default_scaling_option_two_stage_zne():
qreg = cirq.LineQubit.range(2)
cirq_circuit = cirq.Circuit(
cirq.H.on_each(qreg),
cirq.CNOT(*qreg),
cirq.CNOT(*qreg),
cirq.H.on_each(qreg),
)

scale_factors = [3, 5, 6, 8]

circs_default_scaling_method = scaled_circuits(cirq_circuit, scale_factors)

for i in range(len(scale_factors)):
assert len(circs_default_scaling_method[i]) > len(cirq_circuit)
2 changes: 1 addition & 1 deletion mitiq/zne/zne.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
def scaled_circuits(
circuit: QPROGRAM,
scale_factors: list[float],
scale_method: Callable[[QPROGRAM, float], QPROGRAM],
scale_method: Callable[[QPROGRAM, float], QPROGRAM] = fold_gates_at_random, # type:ignore [has-type]
) -> list[QPROGRAM]:
"""Given a circuit, scale_factors and a scale_method, outputs a list
of circuits that will be used in ZNE.
Expand Down

0 comments on commit c1be865

Please sign in to comment.