Skip to content

Commit

Permalink
Migrate to EstimatorV2 in tests (#644)
Browse files Browse the repository at this point in the history
* Migrate to `EstimatorV2` in tests

Partially addresses #506.

* black

* Replace `observables` with `observables_nophase`

The previous code passed `observables_nophase` to the cutting code,
so it wasn't even testing the phases through the workflow.  The
correct way to handle such observables with nontrivial phase is to
use the utility introduced in #587.
  • Loading branch information
garrison authored Jul 19, 2024
1 parent c3b05ed commit cad1f2d
Showing 1 changed file with 9 additions and 13 deletions.
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

0 comments on commit cad1f2d

Please sign in to comment.