Skip to content

Conversation

ddddddanni
Copy link
Collaborator

Previous PR is #7435
This PR allow user to run measuring_pauli_strings in sweep mode. In this mode, the function uses parameterized circuits and sweeps parameters for both Pauli measurements and readout benchmarking

@github-actions github-actions bot added the size: L 250< lines changed <1000 label Aug 12, 2025
@ddddddanni ddddddanni marked this pull request as ready for review August 14, 2025 03:31
@ddddddanni ddddddanni requested review from vtomole and a team as code owners August 14, 2025 03:31
@ddddddanni ddddddanni requested a review from maffoo August 14, 2025 03:31
@eliottrosenberg eliottrosenberg self-requested a review August 14, 2025 16:23
Copy link
Collaborator

@eliottrosenberg eliottrosenberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested this on hardware, and it seems to have a bug. For example, compare the last line of the output of this cell to the output of this cell. result and result2 come from the couple of cells above those. Reproduced again futher down in that colab.

Copy link
Collaborator

@eliottrosenberg eliottrosenberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I found the difference. If you start with the following circuit:

0: ───────────────────────H───@───H───
                              │
1: ───────────────H───@───H───@───────
                      │
2: ───H───────@───H───@───────────────
              │
3: ───H───@───@───────────────────────
          │
4: ───H───@───────H───@───────────────
                      │
5: ───────────────H───@───H───@───────
                              │
6: ───────────────────────H───@───H───

and then measure X(q(0))*X(q(1))*X(q(2))*X(q(3))*X(q(4))*X(q(5))*X(q(6)), the sweep version gives you

0: ───────────────────────H───@───H───PhX(phi0/2 - 1/2)^theta0───M('m')───
                              │                                  │
1: ───────────────H───@───H───@───────PhX(phi1/2 - 1/2)^theta1───M────────
                      │                                          │
2: ───H───────@───H───@───────────────PhX(phi2/2 - 1/2)^theta2───M────────
              │                                                  │
3: ───H───@───@───────────────────────PhX(phi3/2 - 1/2)^theta3───M────────
          │                                                      │
4: ───H───@───────H───@───────────────PhX(phi4/2 - 1/2)^theta4───M────────
                      │                                          │
5: ───────────────H───@───H───@───────PhX(phi5/2 - 1/2)^theta5───M────────
                              │                                  │
6: ───────────────────────H───@───H───PhX(phi6/2 - 1/2)^theta6───M────────

with the basis change all in the same moment, whereas in the batch version, you get

0: ───────────────────────────────H───────────@───H───────────Ry(-0.5π)───M('m')───
                                              │                           │
1: ───────────────H───────────@───H───────────@───Ry(-0.5π)───────────────M────────
                              │                                           │
2: ───H───────@───H───────────@───Ry(-0.5π)───────────────────────────────M────────
              │                                                           │
3: ───H───@───@───Ry(-0.5π)───────────────────────────────────────────────M────────
          │                                                               │
4: ───H───@───────H───────────@───Ry(-0.5π)───────────────────────────────M────────
                              │                                           │
5: ───────────────H───────────@───H───────────@───Ry(-0.5π)───────────────M────────
                                              │                           │
6: ───────────────────────────────H───────────@───H───────────Ry(-0.5π)───M────────

with the basis change done as early as possible. In this case, you get a higher-fidelity result with the basis change done as early as possible because you are insensitive to the dephasing that happens between the final basis change gate and the measurement, but that may not always be the intended behavior. Maybe we could add an insert_strategy argument so that users have the choice of whether to do the basis change all in the same penultimate moment or as early as possible.

@eliottrosenberg
Copy link
Collaborator

Thanks, @ddddddanni! It looks like it is printing out the circuits for debugging, and you might want to remove that.

Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@github-actions github-actions bot added size: XL lines changed >1000 and removed size: L 250< lines changed <1000 labels Aug 25, 2025
@github-actions github-actions bot added size: L 250< lines changed <1000 and removed size: XL lines changed >1000 labels Aug 25, 2025
@ddddddanni
Copy link
Collaborator Author

Thanks, @ddddddanni! It looks like it is printing out the circuits for debugging, and you might want to remove that.

Done!

@github-actions github-actions bot added size: M 50< lines changed <250 size: XL lines changed >1000 and removed size: L 250< lines changed <1000 size: M 50< lines changed <250 labels Sep 1, 2025
@github-actions github-actions bot added size: L 250< lines changed <1000 and removed size: XL lines changed >1000 labels Sep 1, 2025
Copy link

codecov bot commented Sep 1, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.37%. Comparing base (6816824) to head (121286c).
⚠️ Report is 7 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff            @@
##             main    #7569    +/-   ##
========================================
  Coverage   99.37%   99.37%            
========================================
  Files        1078     1082     +4     
  Lines       96166    96761   +595     
========================================
+ Hits        95563    96157   +594     
- Misses        603      604     +1     

☔ 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.

Copy link
Collaborator

@eliottrosenberg eliottrosenberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some things that @ddddddanni and I will keep investigating, but I think we can go ahead and merge this. I just want to clarify that this still has shuffling for the run_batch case, correct? Thanks!

@ddddddanni
Copy link
Collaborator Author

There are some things that @ddddddanni and I will keep investigating, but I think we can go ahead and merge this. I just want to clarify that this still has shuffling for the run_batch case, correct? Thanks!

Sure! I have re-enabled shuffling function

@ddddddanni
Copy link
Collaborator Author

@NoureldinYosri can you please review this PR? Thank you!!

return operations


def _pauli_strings_to_basis_change_with_sweep(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm having a déjà vu moment, didn't you write a similar method before?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a giant PR https://github.com/quantumlib/Cirq/pull/7358/files which you reviewed long time ago. I break that PR into smaller ones: #7435 and this pr, per you requested.
For question you asked in #7358: https://screenshot.googleplex.com/6HHTPQdj3R6Vwfy and https://screenshot.googleplex.com/9kw8sR9Q8g4BnMF - each qubit can belong to many input pauli strings. Since input pauli strings are qubit-wise commuting, it's safe to do so. For example, input pauli strings could be {X(q_0)I(q_1), I(q_0)X(q_1)}, and this function returns params_dict{q_0:x basis change, q_1: x basis change}.

basis_change_circuits = []
input_circuit_unfrozen = input_circuit.unfreeze()
for pauli_strings in pauli_string_groups:
basis_change_circuit = circuits.Circuit(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that we don't really need an insert_strategy, we can just do

circuits.Circuit.from_moments(
     *input_circuit,  
     _pauli_strings_to_basis_change_ops(pauli_strings, qid_list),
    ops.measure(*qid_list, key="m"),
)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to give users choices of whether to do the basis change all in the same penultimate moment or as early as possible. This was discussed https://screenshot.googleplex.com/4LPKcw4eYCSvP85.

basis_change_circuit = circuits.Circuit(
input_circuit_unfrozen,
_pauli_strings_to_basis_change_ops(pauli_strings, qid_list),
ops.measure(*qid_list, key="m"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets use a more meaningful key name

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

]
measurement_op = ops.M(*qid_list, key="m")

parameterized_circuit = circuits.Circuit(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.from_moments

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same above, we may want to allow users to control the insert strategy of basis changes.

sweep_params = []
for bitstr in random_bitstrings:
sweep_params.append({exp: bit for exp, bit in zip(exp_symbols, bitstr)})
sweep_params.append({exp.name: bit for exp, bit in zip(exp_symbols, bitstr)})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about str(exp)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: L 250< lines changed <1000
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants