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

Migrate to EstimatorV2 in tests #644

Merged
merged 3 commits into from
Jul 19, 2024
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: 9 additions & 13 deletions test/cutting/test_cutting_roundtrip.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@
DCXGate,
)
from qiskit.quantum_info import PauliList, random_unitary
from qiskit.primitives import Estimator
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit_ibm_runtime import SamplerV2
from qiskit_aer import AerSimulator
from qiskit_aer.primitives import Sampler
from qiskit_aer.primitives import Sampler, EstimatorV2

from circuit_knitting.utils.simulation import ExactSampler
from circuit_knitting.cutting import (
Expand Down Expand Up @@ -138,16 +138,15 @@ def test_cutting_exact_reconstruction(example_circuit):
"""Test gate-cut circuit vs original circuit on statevector simulator"""
qc = example_circuit

observables = PauliList(["III", "IIY", "XII", "XYZ", "iZZZ", "-XZI"])
phases = np.array([(-1j) ** obs.phase for obs in observables])
observables_nophase = PauliList(["III", "IIY", "XII", "XYZ", "ZZZ", "XZI"])
observables = PauliList(["III", "IIY", "XII", "XYZ", "ZZZ", "XZI"])

estimator = Estimator()
estimator = EstimatorV2()
pm = generate_preset_pass_manager(optimization_level=1, basis_gates=["u", "cz"])
exact_expvals = (
estimator.run([qc] * len(observables), list(observables)).result().values
estimator.run([(pm.run(qc), list(observables))]).result()[0].data.evs
)
subcircuits, bases, subobservables = partition_problem(
qc, "AAB", observables=observables_nophase
qc, "AAB", observables=observables
)
subexperiments, coefficients = generate_cutting_experiments(
subcircuits, subobservables, num_samples=np.inf
Expand All @@ -166,7 +165,6 @@ def test_cutting_exact_reconstruction(example_circuit):
reconstructed_expvals = reconstruct_expectation_values(
results, coefficients, subobservables
)
reconstructed_expvals *= phases

logger.info("Max error: %f", np.max(np.abs(exact_expvals - reconstructed_expvals)))

Expand Down Expand Up @@ -216,10 +214,8 @@ def test_sampler_with_identity_subobservable(sampler, is_exact_sampler):

if is_exact_sampler:
# Determine exact expectation values
estimator = Estimator()
exact_expvals = (
estimator.run([qc] * len(observables), list(observables)).result().values
)
estimator = EstimatorV2()
exact_expvals = estimator.run([(qc, list(observables))]).result()[0].data.evs

logger.info(
"Max error: %f", np.max(np.abs(exact_expvals - reconstructed_expvals))
Expand Down