Skip to content

Conversation

Aqil-Ahmad
Copy link
Contributor

Summary

This PR enhances the ClassicalStateSimulator by adding support for QubitPermutationGate operations and enabling tuple[int] input for the initial state parameter.

Fixes : #7566

Changes Made

  • QubitPermutationGate Support: Implemented handling of qubit permutation operations in _act_on_fallback_ method
  • Tuple Input Support: Added support for tuple[int, ...] as initial state input alongside existing int, list[int], and np.ndarray options
  • Documentation Updates: Updated docstrings and error messages
  • Testing: Added test coverage for:
    • QubitPermutationGate operations with various permutations
    • Tuple input validation and functionality
    • Edge cases and error conditions

Testing

  • All existing tests pass
  • New tests for QubitPermutationGate functionality and tuple input handling

@Aqil-Ahmad Aqil-Ahmad requested review from a team and vtomole as code owners August 11, 2025 13:50
@Aqil-Ahmad Aqil-Ahmad requested a review from viathor August 11, 2025 13:50
Copy link

google-cla bot commented Aug 11, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@github-actions github-actions bot added the size: M 50< lines changed <250 label Aug 11, 2025
Copy link

codecov bot commented Aug 11, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.50%. Comparing base (c517794) to head (a718a2c).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #7567   +/-   ##
=======================================
  Coverage   97.50%   97.50%           
=======================================
  Files        1103     1103           
  Lines       99683    99707   +24     
=======================================
+ Hits        97198    97222   +24     
  Misses       2485     2485           

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

def __init__(
self,
initial_state: int | list[int] = 0,
initial_state: int | list[int] | tuple[int, ...] = 0,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
initial_state: int | list[int] | tuple[int, ...] = 0,
initial_state: int | Sequence[int] = 0,

Args:
qubits: The qubits to simulate.
initial_state: The initial state for the simulation.
initial_state: The initial state for the simulation. Accepts int, list[int],
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
initial_state: The initial state for the simulation. Accepts int, list[int],
initial_state: The initial state for the simulation. Accepts int or a sequence of int.

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.

please check the definition of the permutation gate https://quantumai.google/reference/python/cirq/QubitPermutationGate

and make sure the ClassicalStateSimulator is consistent with the cirq.Simulator

@Aqil-Ahmad Aqil-Ahmad force-pushed the classical-sim-qubit-permutation-tuple-support branch 2 times, most recently from 3d997b8 to 38408a3 Compare August 12, 2025 18:56
circuit = cirq.Circuit(perm_gate(*qubits), cirq.measure(*qubits, key='key'))
sim = cirq.ClassicalStateSimulator()
result = sim.simulate(circuit, initial_state=state)
expected = [state[perm[i]] for i in range(n)]
Copy link
Collaborator

Choose a reason for hiding this comment

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

this doesn't match the action of the state vector simulator (cirq.Simulator). if you believe the state vector simulator is the wrong one please file a bug, otherwise please make them match

@Aqil-Ahmad Aqil-Ahmad force-pushed the classical-sim-qubit-permutation-tuple-support branch from 38408a3 to d6f095f Compare August 20, 2025 19:54
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.

thanks @Aqil-Ahmad

@NoureldinYosri NoureldinYosri added this pull request to the merge queue Aug 30, 2025
Merged via the queue into quantumlib:main with commit d0e2bc5 Aug 30, 2025
35 checks passed
ddddddanni pushed a commit to ddddddanni/Cirq that referenced this pull request Sep 1, 2025
…ulator (quantumlib#7567)

## Summary
This PR enhances the `ClassicalStateSimulator` by adding support for
`QubitPermutationGate` operations and enabling `tuple[int]` input for
the initial state parameter.

Fixes : quantumlib#7566 

## Changes Made
- **QubitPermutationGate Support**: Implemented handling of qubit
permutation operations in `_act_on_fallback_` method
- **Tuple Input Support**: Added support for `tuple[int, ...]` as
initial state input alongside existing `int`, `list[int]`, and
`np.ndarray` options
- **Documentation Updates**: Updated docstrings and error messages
- **Testing**: Added test coverage for:
  - QubitPermutationGate operations with various permutations
  - Tuple input validation and functionality
  - Edge cases and error conditions

## Testing

- [x]  All existing tests pass
- [x] New tests for QubitPermutationGate functionality and tuple input
handling

---------

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: M 50< lines changed <250

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Update ClassicalStateSimulator to support QubitPermutationGate

2 participants