-
Notifications
You must be signed in to change notification settings - Fork 1.1k
change use of measurements to records in tomography #7421
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
base: main
Are you sure you want to change the base?
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #7421 +/- ##
==========================================
- Coverage 98.69% 98.68% -0.01%
==========================================
Files 1112 1112
Lines 97957 97957
==========================================
- Hits 96679 96673 -6
- Misses 1278 1284 +6 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
short and sweet. cc @mhucka since you were on the linked issue thread. |
@@ -529,16 +529,16 @@ def single_qubit_state_tomography( | |||
""" | |||
circuit_z = circuit + circuits.Circuit(ops.measure(qubit, key='z')) | |||
results = sampler.run(circuit_z, repetitions=repetitions) | |||
rho_11 = np.mean(results.measurements['z']) | |||
rho_11 = np.mean(results.records['z']) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will take the mean over all instances of measurements with key 'z'
, which will not do the right thing if there are multiple such measurements. I'd suggest either explicitly taking the last measurement (rho_11 = np.mean(results.records['z'][:, -1, :])
) or else failing if there is more that one such measurement (which is what accessing results.measurements
does).
Perhaps a better overall fix would be for this tomography routine to pick a unique key name for the final tomographic measurement that does not coincide with any existing measurement keys in the circuit. This would sidestep the issue of results.records
vs result.measurements
altogether since we would be guaranteed that the tomo key is only used once in the circuit and so using results.measurements
would be fine. For example, could do something like:
keys = cirq.measurement_key_names(circuit)
tomo_key = "tomo_key"
while tomo_key in keys:
tomo_key = f"tomo_key{uuid.uuid4().hex}"
circuit_z = circuit + circuits.Circuit(ops.measure(qubit, key=tomo_key))
...
@@ -529,16 +529,16 @@ def single_qubit_state_tomography( | |||
""" | |||
circuit_z = circuit + circuits.Circuit(ops.measure(qubit, key='z')) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do think it would be good to check here whether the key 'z'
is being used in the given circuit, and if so pick a unique key. One thing that could still fail is if the key is already used in the circuit to measure two or more qubits (e.g. cirq.measure(q0, q1, key='z')
) then adding another instance here with a single qubit will cause errors when running the circuit.
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
closes #7417