Skip to content

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

kygnz
Copy link

@kygnz kygnz commented Jun 13, 2025

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

@kygnz kygnz requested review from mrwojtek, vtomole and a team as code owners June 13, 2025 01:46
@kygnz kygnz requested a review from NoureldinYosri June 13, 2025 01:46
@github-actions github-actions bot added the Size: XS <10 lines changed label Jun 13, 2025
Copy link

codecov bot commented Jun 13, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.68%. Comparing base (5ea1d04) to head (6cfe17d).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@daxfohl
Copy link
Collaborator

daxfohl commented Jun 16, 2025

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'])
Copy link
Contributor

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'))
Copy link
Contributor

@maffoo maffoo Jun 18, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Size: XS <10 lines changed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

single_qubit_state_tomography issues with multiple key measurements
4 participants