Skip to content
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

Adds tools for appending randomized measurement bases and processing renyi entropy from bitstring #6664

Merged

Conversation

senecameeks
Copy link
Collaborator

Requested by our NISQ team, this PR:

  1. Adds a new RandomizedMeasurement class to the experiment folder.
  2. Adds a tool in cirq/qis for computing the renyi entropy. The technique is well-known and was used by our team, for example in Satzinger et al (2021), Eq. S4 of the SM.

Notably this PR does not create a new transformer since append_random_measurements returns a sequence of circuits and thus violates the transformer api contract.

CI passes once #6662 is merged

@senecameeks senecameeks requested review from mrwojtek, vtomole, cduck and a team as code owners July 10, 2024 18:22
@CirqBot CirqBot added the size: L 250< lines changed <1000 label Jul 10, 2024
Copy link

codecov bot commented Jul 10, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.82%. Comparing base (3922a63) to head (c92e7c9).

Additional details and impacted files
@@           Coverage Diff            @@
##             main    #6664    +/-   ##
========================================
  Coverage   97.82%   97.82%            
========================================
  Files        1068     1072     +4     
  Lines       91919    92029   +110     
========================================
+ Hits        89917    90027   +110     
  Misses       2002     2002            

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

num_unitaries: int,
subsystem: Sequence[str | int] | None = None,
qubit_mapping: Mapping[int, str | int] | None = None,
rng: np.random.Generator | None = None,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I know it's a common code pattern in Cirq but it's a harmful practice. please pass rng to unitaries_to_moment instead

@eliottrosenberg
Copy link
Collaborator

Notably this PR does not create a new transformer since append_random_measurements returns a sequence of circuits and thus violates the transformer api contract.

This could be written as a transformer that just appends the random measurements once; then append_randomized_measurements would just call that transformer num_unitaries times. That is the strategy that I took in #6665, which is also meant to be called many times.

cirq-core/cirq/qis/__init__.py Outdated Show resolved Hide resolved
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.

Thanks, Seneca! This is getting very close; just some small suggestions.

cirq-core/cirq/qis/entropy.py Outdated Show resolved Hide resolved
cirq-core/cirq/qis/entropy.py Outdated Show resolved Hide resolved
pre_measurement_moment = self.unitaries_to_moment(pre_measurement_unitaries_list, qubits)

return cirq.Circuit.from_moments(
*circuit.moments, pre_measurement_moment, cirq.measure_each(*qubits)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe instead of cirq.measure_each(*qubits) we should use cirq.M(*qubits, key='m') to make it easier to extract the bitstrings to feed into process_renyi_entropy_from_bitstrings. (As it is, each qubit has a separate measurement key; this would give one key for the multi-qubit measurement.)

@senecameeks senecameeks enabled auto-merge (squash) July 18, 2024 00:01
Copy link
Collaborator

@NoureldinYosri NoureldinYosri left a comment

Choose a reason for hiding this comment

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

just small nit


@transformer_api.transformer
class RandomizedMeasurements:
"""A transformer that appends a moment of random rotations to map qubits to
Copy link
Collaborator

Choose a reason for hiding this comment

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

I didn't notice that you also add the measurement operation.

Suggested change
"""A transformer that appends a moment of random rotations to map qubits to
"""A transformer that appends necessery operations to measure in random pauli bases."""

@senecameeks senecameeks merged commit 75b3f40 into quantumlib:main Jul 18, 2024
32 checks passed
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this pull request Oct 31, 2024
…renyi entropy from bitstring (quantumlib#6664)

* add utilities for processing renyi entropy and appending randomized measurements + tests

* nit: update test comments

* address comments

* fix np array shape which changes test solution

* Address comments and fix bug

* address comments

* use zip and itertools and update to transformer

* rm print

* type check

* Update cirq-core/cirq/qis/entropy.py

Co-authored-by: Noureldin <[email protected]>

* Update cirq-core/cirq/transformers/randomized_measurements.py

Co-authored-by: Noureldin <[email protected]>

* comments

* line too long

---------

Co-authored-by: Noureldin <[email protected]>
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.

4 participants