diff --git a/pytket/pytket/utils/expectations.py b/pytket/pytket/utils/expectations.py index 7193f665a0..88cdc2ad1b 100644 --- a/pytket/pytket/utils/expectations.py +++ b/pytket/pytket/utils/expectations.py @@ -101,16 +101,16 @@ def get_pauli_expectation_value( state_circuit = backend.get_compiled_circuit(state_circuit) if backend.supports_expectation: return backend.get_pauli_expectation_value(state_circuit, pauli) # type: ignore - handle = backend.process_circuit(state_circuit) - state = backend.get_state(handle) - backend.pop_result(handle) + result = backend.run_circuit(state_circuit) + state = result.get_state(handle) return complex(pauli.state_expectation(state)) measured_circ = state_circuit.copy() append_pauli_measurement(pauli, measured_circ) measured_circ = backend.get_compiled_circuit(measured_circ) if backend.supports_counts: - counts = backend.get_counts(measured_circ, n_shots=n_shots) + result = backend.run_circuit(measured_circ, n_shots=n_shots) + counts = result.get_counts(measured_circ) return expectation_from_counts(counts) elif backend.supports_shots: shot_table = backend.get_shots(measured_circ, n_shots=n_shots) @@ -161,7 +161,8 @@ def get_operator_expectation_value( backend.expectation_allows_nonhermitian or all(z.imag == 0 for z in coeffs) ): return backend.get_operator_expectation_value(state_circuit, operator) # type: ignore - state = backend.get_state(state_circuit) + result = backend.run_circuit(state_circuit) + state = result.get_state() return operator.state_expectation(state) energy: complex id_string = QubitPauliString() diff --git a/pytket/tests/backend_test.py b/pytket/tests/backend_test.py index a9cb432bba..c4a66f5605 100644 --- a/pytket/tests/backend_test.py +++ b/pytket/tests/backend_test.py @@ -75,7 +75,8 @@ def test_bell() -> None: c.CX(0, 1) assert np.allclose(c.get_statevector(), sv) b = TketSimBackend() - assert np.allclose(b.get_state(c), sv) + r = b.run_circuit(c) + assert np.allclose(r.get_state(), sv) # Check that the "get_compiled_circuit" result is still the same. # (The circuit could change, due to optimisations). c = b.get_compiled_circuit(c) @@ -90,13 +91,15 @@ def test_basisorder() -> None: b = TketSimBackend() c = Circuit(2) c.X(1) - assert (b.get_state(c) == np.asarray([0, 1, 0, 0])).all() - assert (b.get_state(c, basis=BasisOrder.dlo) == np.asarray([0, 0, 1, 0])).all() + r = b.run_circuit(c) + assert (r.get_state() == np.asarray([0, 1, 0, 0])).all() + assert (r.get_state(basis=BasisOrder.dlo) == np.asarray([0, 0, 1, 0])).all() assert (c.get_statevector() == np.asarray([0, 1, 0, 0])).all() b = TketSimShotBackend() c.measure_all() - assert b.get_shots(c, n_shots=4, seed=4).shape == (4, 2) - assert b.get_counts(c, n_shots=4, seed=4) == {(0, 1): 4} + r = b.run_circuit(c, n_shots=4, seed=4) + assert r.get_shots().shape == (4, 2) + assert r.get_counts() == {(0, 1): 4} @pytest.mark.filterwarnings("ignore::PendingDeprecationWarning") @@ -112,8 +115,9 @@ def test_swaps() -> None: bs = TketSimBackend() c, c1 = bs.get_compiled_circuits((c, c1)) - s = bs.get_state(c) - s1 = bs.get_state(c1) + [r, r1] = bs.run_circuits([c, c1]) + s = r.get_state() + s1 = r1.get_state() assert np.allclose(s, s1) assert np.allclose(s_direct, s) assert np.allclose(s1_direct, s) @@ -160,15 +164,16 @@ def test_swaps_basisorder() -> None: b = TketSimBackend() c, c1 = b.get_compiled_circuits((c, c1)) - s_ilo = b.get_state(c1, basis=BasisOrder.ilo) - correct_ilo = b.get_state(c, basis=BasisOrder.ilo) + r, r1 = b.run_circuits((c, c1)) + s_ilo = r1.get_state(basis=BasisOrder.ilo) + correct_ilo = r.get_state(basis=BasisOrder.ilo) assert np.allclose(s_ilo, correct_ilo) assert np.allclose(s_ilo_direct, s_ilo) assert np.allclose(correct_ilo_direct, s_ilo) - s_dlo = b.get_state(c1, basis=BasisOrder.dlo) - correct_dlo = b.get_state(c, basis=BasisOrder.dlo) + s_dlo = r1.get_state(basis=BasisOrder.dlo) + correct_dlo = r.get_state(basis=BasisOrder.dlo) assert np.allclose(s_dlo, correct_dlo) qbs = c.qubits @@ -440,7 +445,8 @@ def test_tket_sim_backend_equivalence_with_circuit_functions() -> None: states = [circ.get_statevector(), compiled_circ.get_statevector()] unitaries = [circ.get_unitary(), compiled_circ.get_unitary()] - states.append(backend.get_state(compiled_circ)) + result = backend.run_circuit(compiled_circ) + states.append(result.get_state()) # paranoia: get_state should not alter the circuit states.append(compiled_circ.get_statevector()) diff --git a/pytket/tests/utils_test.py b/pytket/tests/utils_test.py index bc4d4cfabe..00745837a6 100644 --- a/pytket/tests/utils_test.py +++ b/pytket/tests/utils_test.py @@ -541,7 +541,7 @@ def unitary_from_states(circ: Circuit, back: Backend) -> np.ndarray: if val == "1": basis_circ.X(qb) basis_circ.append(circ) - outar[:, i] = back.get_state(basis_circ) + outar[:, i] = back.run_circuit(basis_circ).get_state() return outar @@ -582,7 +582,8 @@ def test_symbolic_conversion(circ: Circuit) -> None: back = TketSimBackend() circ = back.get_compiled_circuit(circ, 1) - simulated_state = back.get_state(circ) + result = back.run_circuit(circ) + simulated_state = result.get_state() assert np.allclose(numeric_state.T, simulated_state, atol=1e-10) simulated_unitary = unitary_from_states(circ, back)