Skip to content

fix: Handle Projection and Reset in DensityMatrixSimulation#376

Open
speller26 wants to merge 3 commits into
mainfrom
dm
Open

fix: Handle Projection and Reset in DensityMatrixSimulation#376
speller26 wants to merge 3 commits into
mainfrom
dm

Conversation

@speller26
Copy link
Copy Markdown
Member

Issue #, if available:

Description of changes:

Testing done:

Merge Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your pull request.

General

Tests

  • I have added tests that prove my fix is effective or that my feature works (if appropriate)
  • I have checked that my tests are not configured for a specific region or account (if appropriate)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@speller26 speller26 requested a review from a team as a code owner May 14, 2026 17:58
@codecov
Copy link
Copy Markdown

codecov Bot commented May 14, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (e12f376) to head (bf56dbc).

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #376   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           49        49           
  Lines         4882      4897   +15     
  Branches       561       563    +2     
=========================================
+ Hits          4882      4897   +15     

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

yitchen-tim
yitchen-tim previously approved these changes May 15, 2026
@yitchen-tim yitchen-tim dismissed their stale review May 15, 2026 13:11

find the edge case

@yitchen-tim
Copy link
Copy Markdown
Contributor

Found an edge case:

qreg = [8,13,17,18]

with EnableExperimentalCapability():
    circ = Circuit()
    circ.h(qreg[1])
    circ.measure_ff(qreg[1], 0)
    circ.cc_prx(qreg[2], 3.14159, 0, 0)
    circ.measure([qreg[1],qreg[2]])

print(circ)
res_cnot = qd.run(circ, shots=1000).result()
print(res_cnot.measurement_counts)

with error stack

File [~/Desktop/code/Github/amazon-braket-default-simulator-python/src/braket/default_simulator/density_matrix_simulation.py:50](http://localhost:8888/lab/tree/examples/experimental_capabilities/dynamic_circuits/~/Desktop/code/Github/amazon-braket-default-simulator-python/src/braket/default_simulator/density_matrix_simulation.py#line=49), in DensityMatrixSimulation.evolve(self, operations)
     49 def evolve(self, operations: list[GateOperation | KrausOperation]) -> None:
---> 50     self._density_matrix = DensityMatrixSimulation._apply_operations(
     51         self._density_matrix, self._qubit_count, operations
     52     )

File [~/Desktop/code/Github/amazon-braket-default-simulator-python/src/braket/default_simulator/density_matrix_simulation.py:152](http://localhost:8888/lab/tree/examples/experimental_capabilities/dynamic_circuits/~/Desktop/code/Github/amazon-braket-default-simulator-python/src/braket/default_simulator/density_matrix_simulation.py#line=151), in DensityMatrixSimulation._apply_operations(state, qubit_count, operations)
    150 original_shape = state.shape
    151 result = state.view()
--> 152 result.shape = [2] * 2 * qubit_count
    153 temp = np.zeros_like(result, dtype=complex)
    154 work_buffer1 = np.zeros_like(result, dtype=complex)

ValueError: maximum supported dimension for an ndarray is 32, found 36

I think there is something wrong in setting qubit_count. It should be only 2 qubits but it uses the max qubit index as qubit count (=18 qubits).

@speller26
Copy link
Copy Markdown
Member Author

Found an edge case:

qreg = [8,13,17,18]

with EnableExperimentalCapability():
    circ = Circuit()
    circ.h(qreg[1])
    circ.measure_ff(qreg[1], 0)
    circ.cc_prx(qreg[2], 3.14159, 0, 0)
    circ.measure([qreg[1],qreg[2]])

print(circ)
res_cnot = qd.run(circ, shots=1000).result()
print(res_cnot.measurement_counts)

with error stack

File [~/Desktop/code/Github/amazon-braket-default-simulator-python/src/braket/default_simulator/density_matrix_simulation.py:50](http://localhost:8888/lab/tree/examples/experimental_capabilities/dynamic_circuits/~/Desktop/code/Github/amazon-braket-default-simulator-python/src/braket/default_simulator/density_matrix_simulation.py#line=49), in DensityMatrixSimulation.evolve(self, operations)
     49 def evolve(self, operations: list[GateOperation | KrausOperation]) -> None:
---> 50     self._density_matrix = DensityMatrixSimulation._apply_operations(
     51         self._density_matrix, self._qubit_count, operations
     52     )

File [~/Desktop/code/Github/amazon-braket-default-simulator-python/src/braket/default_simulator/density_matrix_simulation.py:152](http://localhost:8888/lab/tree/examples/experimental_capabilities/dynamic_circuits/~/Desktop/code/Github/amazon-braket-default-simulator-python/src/braket/default_simulator/density_matrix_simulation.py#line=151), in DensityMatrixSimulation._apply_operations(state, qubit_count, operations)
    150 original_shape = state.shape
    151 result = state.view()
--> 152 result.shape = [2] * 2 * qubit_count
    153 temp = np.zeros_like(result, dtype=complex)
    154 work_buffer1 = np.zeros_like(result, dtype=complex)

ValueError: maximum supported dimension for an ndarray is 32, found 36

I think there is something wrong in setting qubit_count. It should be only 2 qubits but it uses the max qubit index as qubit count (=18 qubits).

Fixed in #378

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants