Skip to content

Commit 315f026

Browse files
Change use of measurements to records in tomography [continued] (#7477)
Bringing PR #7421 across the finish line since the original contributors' internships have concluded. I can't edit the original PR, so forking and opening another PR would be the most efficient. > In the file qubit_characterizations.py, in the function definition of single_qubit_state_tomography, changing the use of measurements to records allows for circuits with multiple key measurements to be supported without throwing an error for having repeated keys Heavy lifting by @kygnz @sgavi4 @abyssaldragonz @thomasasha Closes issue #7417 and the original PR #7421 --------- Co-authored-by: Victory Omole <[email protected]>
1 parent cd32133 commit 315f026

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

cirq-core/cirq/experiments/qubit_characterizations.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import dataclasses
1818
import functools
1919
import itertools
20+
import uuid
2021
from typing import Any, cast, Iterator, Mapping, Sequence, TYPE_CHECKING
2122

2223
import attrs
@@ -631,18 +632,24 @@ def single_qubit_state_tomography(
631632
Returns:
632633
A TomographyResult object that stores and plots the density matrix.
633634
"""
634-
circuit_z = circuit + circuits.Circuit(ops.measure(qubit, key='z'))
635+
keys = protocols.measurement_key_names(circuit)
636+
tomo_key = "tomo_key"
637+
while tomo_key in keys:
638+
tomo_key = f"tomo_key{uuid.uuid4().hex}"
639+
640+
circuit_z = circuit + circuits.Circuit(ops.measure(qubit, key=tomo_key))
641+
635642
results = sampler.run(circuit_z, repetitions=repetitions)
636-
rho_11 = np.mean(results.measurements['z'])
643+
rho_11 = np.mean(results.records[tomo_key][:, -1, :])
637644
rho_00 = 1.0 - rho_11
638645

639-
circuit_x = circuits.Circuit(circuit, ops.X(qubit) ** 0.5, ops.measure(qubit, key='z'))
646+
circuit_x = circuits.Circuit(circuit, ops.X(qubit) ** 0.5, ops.measure(qubit, key=tomo_key))
640647
results = sampler.run(circuit_x, repetitions=repetitions)
641-
rho_01_im = np.mean(results.measurements['z']) - 0.5
648+
rho_01_im = np.mean(results.records[tomo_key][:, -1, :]) - 0.5
642649

643-
circuit_y = circuits.Circuit(circuit, ops.Y(qubit) ** -0.5, ops.measure(qubit, key='z'))
650+
circuit_y = circuits.Circuit(circuit, ops.Y(qubit) ** -0.5, ops.measure(qubit, key=tomo_key))
644651
results = sampler.run(circuit_y, repetitions=repetitions)
645-
rho_01_re = 0.5 - np.mean(results.measurements['z'])
652+
rho_01_re = 0.5 - np.mean(results.records[tomo_key][:, -1, :])
646653

647654
rho_01 = rho_01_re + 1j * rho_01_im
648655
rho_10 = np.conj(rho_01)

cirq-core/cirq/experiments/qubit_characterizations_test.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,24 +157,34 @@ def test_two_qubit_randomized_benchmarking():
157157
def test_single_qubit_state_tomography():
158158
# Check that the density matrices of the output states of X/2, Y/2 and
159159
# H + Y gates closely match the ideal cases.
160+
# Checks that unique tomography keys are generated
160161
simulator = sim.Simulator()
161-
qubit = GridQubit(0, 0)
162+
q_0 = GridQubit(0, 0)
163+
q_1 = GridQubit(0, 1)
162164

163-
circuit_1 = circuits.Circuit(ops.X(qubit) ** 0.5)
164-
circuit_2 = circuits.Circuit(ops.Y(qubit) ** 0.5)
165-
circuit_3 = circuits.Circuit(ops.H(qubit), ops.Y(qubit))
165+
circuit_1 = circuits.Circuit(ops.X(q_0) ** 0.5)
166+
circuit_2 = circuits.Circuit(ops.Y(q_0) ** 0.5)
167+
circuit_3 = circuits.Circuit(ops.H(q_0), ops.Y(q_0))
168+
circuit_4 = circuits.Circuit(ops.H(q_0), ops.Y(q_0), cirq.measure(q_1, key='z'))
169+
circuit_5 = circuits.Circuit(ops.H(q_0), ops.Y(q_0), cirq.measure(q_1, key='tomo_key'))
166170

167-
act_rho_1 = single_qubit_state_tomography(simulator, qubit, circuit_1, 1000).data
168-
act_rho_2 = single_qubit_state_tomography(simulator, qubit, circuit_2, 1000).data
169-
act_rho_3 = single_qubit_state_tomography(simulator, qubit, circuit_3, 1000).data
171+
act_rho_1 = single_qubit_state_tomography(simulator, q_0, circuit_1, 1000).data
172+
act_rho_2 = single_qubit_state_tomography(simulator, q_0, circuit_2, 1000).data
173+
act_rho_3 = single_qubit_state_tomography(simulator, q_0, circuit_3, 1000).data
174+
act_rho_4 = single_qubit_state_tomography(simulator, q_0, circuit_4, 1000).data
175+
act_rho_5 = single_qubit_state_tomography(simulator, q_0, circuit_5, 1000).data
170176

171177
tar_rho_1 = np.array([[0.5, 0.5j], [-0.5j, 0.5]])
172178
tar_rho_2 = np.array([[0.5, 0.5], [0.5, 0.5]])
173179
tar_rho_3 = np.array([[0.5, -0.5], [-0.5, 0.5]])
180+
tar_rho_4 = np.array([[0.5, -0.5], [-0.5, 0.5]])
181+
tar_rho_5 = np.array([[0.5, -0.5], [-0.5, 0.5]])
174182

175183
np.testing.assert_almost_equal(act_rho_1, tar_rho_1, decimal=1)
176184
np.testing.assert_almost_equal(act_rho_2, tar_rho_2, decimal=1)
177185
np.testing.assert_almost_equal(act_rho_3, tar_rho_3, decimal=1)
186+
np.testing.assert_almost_equal(act_rho_4, tar_rho_4, decimal=1)
187+
np.testing.assert_almost_equal(act_rho_5, tar_rho_5, decimal=1)
178188

179189

180190
def test_two_qubit_state_tomography():

0 commit comments

Comments
 (0)