Skip to content

Commit

Permalink
reflected API changes to tests and utils/expectations
Browse files Browse the repository at this point in the history
  • Loading branch information
lmondada committed Sep 29, 2021
1 parent 611f845 commit de48bbd
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
1 change: 0 additions & 1 deletion pytket/pytket/backends/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
List,
Optional,
Sequence,
Tuple,
Union,
Any,
cast,
Expand Down
11 changes: 5 additions & 6 deletions pytket/pytket/utils/expectations.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,17 @@ 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)
state = backend.run_circuit(state_circuit).get_state()
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)
counts = backend.run_circuit(measured_circ, n_shots=n_shots).get_counts()
return expectation_from_counts(counts)
elif backend.supports_shots:
shot_table = backend.get_shots(measured_circ, n_shots=n_shots)
shot_table = backend.run_circuit(measured_circ, n_shots=n_shots).get_shots()
return expectation_from_shots(shot_table)
else:
raise ValueError("Backend does not support counts or shots")
Expand Down Expand Up @@ -161,7 +159,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()
Expand Down
30 changes: 18 additions & 12 deletions pytket/tests/backend_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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())
Expand Down
5 changes: 3 additions & 2 deletions pytket/tests/utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit de48bbd

Please sign in to comment.