Skip to content

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

Merged
senecameeks merged 15 commits into
quantumlib:mainfrom
senecameeks:u/smeeks/entropy_and_rand_meas
Jul 18, 2024
Merged

Adds tools for appending randomized measurement bases and processing renyi entropy from bitstring#6664
senecameeks merged 15 commits into
quantumlib:mainfrom
senecameeks:u/smeeks/entropy_and_rand_meas

Conversation

@senecameeks

Copy link
Copy Markdown
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 a team, cduck, mrwojtek and vtomole as code owners July 10, 2024 18:22
@CirqBot CirqBot added the size: L 250< lines changed <1000 label Jul 10, 2024
@codecov

codecov Bot commented Jul 10, 2024

Copy link
Copy Markdown

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
Copy Markdown
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

Comment thread cirq-core/cirq/qis/process_renyi_entropy_from_bitstrings.py Outdated
Comment thread cirq-core/cirq/qis/process_renyi_entropy_from_bitstrings.py Outdated
Comment thread cirq-core/cirq/qis/process_renyi_entropy_from_bitstrings.py Outdated
Comment thread cirq-core/cirq/qis/process_renyi_entropy_from_bitstrings.py Outdated
@eliottrosenberg

Copy link
Copy Markdown
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.

Comment thread cirq-core/cirq/experiments/measure_in_random_bases.py Outdated
Comment thread cirq-core/cirq/qis/process_renyi_entropy_from_bitstrings.py Outdated
Comment thread cirq-core/cirq/qis/process_renyi_entropy_from_bitstrings.py Outdated
Comment thread cirq-core/cirq/qis/process_renyi_entropy_from_bitstrings.py Outdated
Comment thread cirq-core/cirq/qis/process_renyi_entropy_from_bitstrings.py Outdated
Comment thread cirq-core/cirq/qis/__init__.py Outdated
Comment thread cirq-core/cirq/qis/entropy.py Outdated
Comment thread cirq-core/cirq/transformers/randomized_measurements.py
Comment thread cirq-core/cirq/qis/entropy.py Outdated
Comment thread cirq-core/cirq/transformers/randomized_measurements.py Outdated

@eliottrosenberg eliottrosenberg left a comment

Copy link
Copy Markdown
Collaborator

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.

Comment thread cirq-core/cirq/qis/entropy.py Outdated
Comment thread cirq-core/cirq/qis/entropy.py Outdated
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
Copy Markdown
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 and others added 4 commits July 17, 2024 16:36
Co-authored-by: Noureldin <noureldinyosri@gmail.com>
Co-authored-by: Noureldin <noureldinyosri@gmail.com>
@senecameeks senecameeks enabled auto-merge (squash) July 18, 2024 00:01

@NoureldinYosri NoureldinYosri left a comment

Copy link
Copy Markdown
Collaborator

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
Copy Markdown
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
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 <noureldinyosri@gmail.com>

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

Co-authored-by: Noureldin <noureldinyosri@gmail.com>

* comments

* line too long

---------

Co-authored-by: Noureldin <noureldinyosri@gmail.com>
BichengYing pushed a commit to BichengYing/Cirq that referenced this pull request Jun 20, 2025
…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 <noureldinyosri@gmail.com>

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

Co-authored-by: Noureldin <noureldinyosri@gmail.com>

* comments

* line too long

---------

Co-authored-by: Noureldin <noureldinyosri@gmail.com>
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