Skip to content

Commit

Permalink
add default option and copy-paste unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
purva-thakre committed Feb 11, 2025
1 parent f404e28 commit bffbbb6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
47 changes: 47 additions & 0 deletions mitiq/zne/tests/test_zne.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,3 +630,50 @@ def executor(circuit):
scale_noise=noise_scaling_method,
)
assert np.isclose(zne_res, two_stage_zne_res)


@pytest.mark.parametrize(
"extrapolation_factory", [RichardsonFactory, LinearFactory]
)
@pytest.mark.parametrize(
"to_frontend",
[None, to_qiskit, to_braket, to_pennylane, to_pyquil, to_qibo],
)
def test_two_stage_zne_default_scaling(extrapolation_factory, to_frontend):
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),
)
if to_frontend is not None:
frontend_circuit = to_frontend(cirq_circuit)
else:
frontend_circuit = cirq_circuit

scale_factors = [1, 3, 5]
circs = scaled_circuits(frontend_circuit, scale_factors)

assert len(circs) == len(scale_factors)

np.random.seed(42)

def executor(circuit):
return np.random.random()

results = [executor(cirq_circuit) for _ in range(3)]
extrapolation_method = extrapolation_factory.extrapolate
two_stage_zne_res = combine_results(
scale_factors, results, extrapolation_method
)

assert isinstance(two_stage_zne_res, float)

np.random.seed(42)
zne_res = execute_with_zne(
cirq_circuit,
executor,
factory=extrapolation_factory(scale_factors),
)
assert np.isclose(zne_res, two_stage_zne_res)
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,
) -> 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 bffbbb6

Please sign in to comment.