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

Native mid-circuit measurements support composite measurements, etc. (phase 2) #5120

Merged
merged 145 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
145 commits
Select commit Hold shift + click to select a range
e9050f5
WIP
vincentmr Jan 19, 2024
587ce7a
Fix mid_circuit_measurements transform.
vincentmr Jan 19, 2024
f680458
Add basic tests.
vincentmr Jan 19, 2024
33efdb5
WIP
vincentmr Jan 22, 2024
c052f39
Add sample & counts support
vincentmr Jan 22, 2024
a6df4cd
Add sample & counts support
vincentmr Jan 22, 2024
f9134f1
Add VarianceMP support.
vincentmr Jan 22, 2024
ca82800
Formatting.
vincentmr Jan 22, 2024
fae6b61
Refactor based on 1-shot.
vincentmr Jan 23, 2024
3bfeb9c
Split gather_native_mid_circuit_measurements
vincentmr Jan 23, 2024
c62f29a
Setup aux circuit in init_auxiliary_circuit.
vincentmr Jan 23, 2024
a386ac0
Add var(obs) support.
vincentmr Jan 23, 2024
c502ea2
Fix for shot_vector
vincentmr Jan 23, 2024
a392c3e
Add optional stopping_condition_shots argument in decompose.
vincentmr Jan 23, 2024
fecbaf7
Force qml.defer_measurements on composite mcm.
vincentmr Jan 23, 2024
d4e8cca
Fix test_observable_is_composite_measurement_value test
vincentmr Jan 23, 2024
21686c4
Fix postselection tests.
vincentmr Jan 24, 2024
436980a
Add reset support.
vincentmr Jan 24, 2024
153fb40
Add reset parametrization for native mcm tests.
vincentmr Jan 24, 2024
77c9708
Fix test since native mcm does not support measurement value lists.
vincentmr Jan 24, 2024
8451282
Fix decorators in defer_measurements tests and use defer_measurements…
vincentmr Jan 24, 2024
17d366a
Merge branch 'master' into feature/mcm
vincentmr Jan 24, 2024
a98e57e
Add few tests for raise error.
vincentmr Jan 24, 2024
1bb7352
Simplify dark_branch flip.
vincentmr Jan 24, 2024
539f9c8
Merge branch 'master' into feature/mcm
vincentmr Jan 24, 2024
44f2a13
Add post-selection support.
vincentmr Jan 24, 2024
8ee4116
Update pennylane/devices/qubit/apply_operation.py
vincentmr Jan 24, 2024
9a014d5
Update pennylane/devices/qubit/simulate.py
vincentmr Jan 24, 2024
7e77e8b
Update pennylane/devices/qubit/simulate.py
vincentmr Jan 24, 2024
f3a38a2
Address Mudit's comment about dark_branch.
vincentmr Jan 24, 2024
eadd0f0
Fix code factor issue.
vincentmr Jan 24, 2024
561a5c0
Update pennylane/devices/qubit/simulate.py
vincentmr Jan 25, 2024
8519ca6
Update changelog.
vincentmr Jan 25, 2024
dbd48a8
Call qml.devices.qubit.measure in apply_mid_measure.
vincentmr Jan 25, 2024
6bdfc23
Update pennylane/devices/qubit/simulate.py
vincentmr Jan 25, 2024
160a960
Implement Mudit suggestions: put things in their own function, add doc.
vincentmr Jan 25, 2024
0221e8e
Merge branch 'master' into feature/mcm
vincentmr Jan 25, 2024
4ba8ba4
Improve mcm test validation.
vincentmr Jan 25, 2024
956aa85
Add postselect tests.
vincentmr Jan 26, 2024
0d5aa37
Update changelog.
vincentmr Jan 26, 2024
84e2e94
Fix black version.
vincentmr Jan 26, 2024
f5ce002
Update changelog-dev.md
vincentmr Jan 26, 2024
a845225
Treat tape.shots as boolean. Import MidMeasureMP from qml.measurement…
vincentmr Jan 26, 2024
c493131
Merge branch 'master' into feature/mcm
vincentmr Jan 26, 2024
0d01872
Add basic composite measurement support.
vincentmr Jan 29, 2024
c567a69
Simplify gather_mcm
vincentmr Jan 29, 2024
a57fa97
Add test for measuring composite MCM.
vincentmr Jan 29, 2024
6aa87d4
Revert defer_meas decorators on measurement tests since natively supp…
vincentmr Jan 29, 2024
815bbfc
Add value list support.
vincentmr Jan 29, 2024
7324c96
Merge branch 'feature/mcm_postselect' into feature/mcm_composite
vincentmr Jan 30, 2024
de4fb47
Merge branch 'master' into feature/mcm
vincentmr Jan 30, 2024
3e80998
Add one more failsafe in apply_mid_meas.
vincentmr Jan 30, 2024
69a7650
Merge branch 'feature/mcm' into feature/mcm_composite
vincentmr Jan 30, 2024
5762cb4
Increase sampling test_composite_mcm_measure_composite_mcm
vincentmr Jan 30, 2024
732e825
Add flaky mark to test_postselection_valid_finite_shots.
vincentmr Jan 30, 2024
135f4d0
Fix no cov. Split core tests into 4 groups.
vincentmr Jan 30, 2024
0975ce0
Split into 5 groups mimicking jax.
vincentmr Jan 30, 2024
ff0421d
Fix setup-ci load.
vincentmr Jan 30, 2024
2937fd0
Add PS gradient tests.
vincentmr Jan 30, 2024
792767a
Fix max_parallel
vincentmr Jan 30, 2024
0c1a96c
Split core tests based on durations.
vincentmr Jan 30, 2024
29ad6d0
Feature/mcm obs (#5128)
vincentmr Jan 30, 2024
d5e7544
Merge branch 'master' into feature/mcm
vincentmr Jan 31, 2024
fd73a65
Feature/mcm process sample (#5134)
vincentmr Feb 1, 2024
7167323
Merge branch 'master' into feature/mcm
vincentmr Feb 1, 2024
cfb1b15
Merge branch 'feature/mcm' into feature/mcm_composite
vincentmr Feb 1, 2024
4295b1f
Merge branch 'master' into feature/mcm
vincentmr Feb 2, 2024
7c239bf
Fix order of qnode/defer_measurements.
vincentmr Feb 2, 2024
42ebb39
Update pennylane/devices/qubit/simulate.py
vincentmr Feb 2, 2024
f4ed364
Fix QuantumScript.
vincentmr Feb 2, 2024
cd8953a
Update pennylane/devices/qubit/simulate.py
vincentmr Feb 2, 2024
59496ff
Update pennylane/devices/qubit/simulate.py
vincentmr Feb 2, 2024
c1d1f19
Update pennylane/devices/qubit/simulate.py
vincentmr Feb 2, 2024
fc0bf92
Update pennylane/devices/qubit/simulate.py
vincentmr Feb 2, 2024
be7ee91
Invert isinstance(all_shot_meas[i], Sequence) condition.
vincentmr Feb 2, 2024
e11043f
Replace has_measurement_values by m.mv.
vincentmr Feb 2, 2024
72ec062
Update pennylane/devices/qubit/simulate.py
vincentmr Feb 2, 2024
454a750
Init all_shot_meas in accumulate_native_mcm.
vincentmr Feb 2, 2024
d188206
Pass mid_measurements dict in apply_op and get_final_state
vincentmr Feb 2, 2024
95d05d5
Merge branch 'master' into feature/mcm
vincentmr Feb 2, 2024
af0ced5
Fix few defer_measure tests.
vincentmr Feb 2, 2024
2732ed7
Merge branch 'master' into feature/mcm
vincentmr Feb 2, 2024
7bb1aa7
Rewrite gather_mcm with singledispatch.
vincentmr Feb 2, 2024
12d4deb
Update pennylane/devices/qubit/apply_operation.py
vincentmr Feb 5, 2024
8fb0e0d
Fix pylint.
vincentmr Feb 5, 2024
db6cb73
Update pennylane/devices/qubit/simulate.py
vincentmr Feb 5, 2024
c58ee71
Update tests/devices/default_qubit/test_default_qubit_native_mcm.py
vincentmr Feb 5, 2024
b9eba43
Replace qml.defer_measurements.
vincentmr Feb 5, 2024
d016fed
Update tests/devices/default_qubit/test_default_qubit_native_mcm.py
vincentmr Feb 5, 2024
cf5a0b7
Use ID instead of hash to tag MCM.
vincentmr Feb 5, 2024
71fa4e5
Fix pylint disable
vincentmr Feb 5, 2024
3c151d7
Merge branch 'master' into feature/mcm
vincentmr Feb 5, 2024
bea47cc
Mark test test_observable_is_measurement_value_list with defer_measur…
vincentmr Feb 5, 2024
9864d02
Mark test test_observable_is_measurement_value_list with defer_measur…
vincentmr Feb 5, 2024
c5cf8a3
mid_measurements=None => **_
vincentmr Feb 5, 2024
1da2495
Merge branch 'master' into feature/mcm
vincentmr Feb 5, 2024
21d6943
Update pennylane/devices/qubit/simulate.py
vincentmr Feb 5, 2024
fe65b2d
Update pennylane/devices/qubit/simulate.py
vincentmr Feb 5, 2024
fa6ef98
Use mv.measurements[0] as key in mcm dict.
vincentmr Feb 6, 2024
f3f8586
Decompose transform need not depend on device.shots.
vincentmr Feb 6, 2024
d6f2a0f
Merge branch 'master' into feature/mcm
vincentmr Feb 6, 2024
327b52c
Merge branch 'master' into feature/mcm
vincentmr Feb 6, 2024
7d25de7
Register apply_conditional as apply_operation.
vincentmr Feb 6, 2024
43b2483
Fix release notes.
vincentmr Feb 6, 2024
12e761b
Update pennylane/devices/preprocess.py
vincentmr Feb 6, 2024
c86f9b1
Merge remote-tracking branch 'origin/feature/mcm' into feature/mcm_co…
vincentmr Feb 7, 2024
670db5d
Merge branch 'master' into feature/mcm
vincentmr Feb 7, 2024
0aadaaa
Update simulate.py
vincentmr Feb 7, 2024
654c715
Remove unused Sequence import.
vincentmr Feb 7, 2024
d480db0
Merge branch 'master' into feature/mcm
vincentmr Feb 7, 2024
4328ff1
Merge branch 'master' into feature/mcm
vincentmr Feb 7, 2024
77d105a
Merge branch 'master' into feature/mcm_composite
vincentmr Feb 7, 2024
aa2a86c
Merge remote-tracking branch 'origin/master' into feature/mcm_composite
vincentmr Feb 7, 2024
f620e5b
Fix counts in mcm meas.
vincentmr Feb 7, 2024
ac21c9a
Update pennylane/devices/qubit/simulate.py
vincentmr Feb 7, 2024
e5cae36
Update pennylane/devices/qubit/simulate.py
vincentmr Feb 7, 2024
3b5770e
Update pennylane/devices/qubit/simulate.py
vincentmr Feb 7, 2024
c71e09e
Fix tests
vincentmr Feb 7, 2024
2976fb8
Update tests/devices/default_qubit/test_default_qubit_native_mcm.py
vincentmr Feb 7, 2024
193cd1b
Update changelog.
vincentmr Feb 8, 2024
0073f67
Update changelog.
vincentmr Feb 8, 2024
244ef3a
Add comment in gather_mcm
vincentmr Feb 8, 2024
015171a
Update tests/devices/default_qubit/test_default_qubit_native_mcm.py
vincentmr Feb 8, 2024
bbd2f34
Update tests/devices/default_qubit/test_default_qubit_native_mcm.py
vincentmr Feb 8, 2024
db80793
Try exiting early when post-selecting states.
vincentmr Feb 8, 2024
b93a565
Revert exit early mods.
vincentmr Feb 8, 2024
fa78e78
Add defer_measurement tests for MPs.
vincentmr Feb 8, 2024
e34b7a4
Merge branch 'master' into feature/mcm_composite
vincentmr Feb 9, 2024
5c35585
Fix indentation in release notes
vincentmr Feb 9, 2024
77dfbba
Update pennylane/devices/qubit/simulate.py
vincentmr Feb 9, 2024
60978ca
Remove find_measurement_values and append to normalized_meas.
vincentmr Feb 9, 2024
fdd7bfe
Merge branch 'master' into feature/mcm_composite
vincentmr Feb 9, 2024
b7f4412
Merge branch 'master' into feature/mcm_composite
vincentmr Feb 12, 2024
3495329
Update tests/devices/default_qubit/test_default_qubit_native_mcm.py
vincentmr Feb 13, 2024
cf86a44
Merge branch 'master' into feature/mcm_composite
vincentmr Feb 13, 2024
00b5746
Add special treatment for all-invalid shots.
vincentmr Feb 13, 2024
8fe1c53
Merge branch 'master' into feature/mcm_composite
vincentmr Feb 13, 2024
fcbc789
Combine conditions to return nan
vincentmr Feb 14, 2024
44db2a2
Merge branch 'master' into feature/mcm_composite
vincentmr Feb 14, 2024
6879d44
reintroduce measurement_with_no_shots
vincentmr Feb 14, 2024
d83dc10
Merge branch 'master' into feature/mcm_composite
vincentmr Feb 14, 2024
268bde3
Remove unattainable parts of the code.
vincentmr Feb 15, 2024
3cb7121
Merge branch 'master' into feature/mcm_composite
vincentmr Feb 15, 2024
a426772
new_shot_meas = list(one_shot_meas)
vincentmr Feb 16, 2024
bd9b448
Merge branch 'master' into feature/mcm_composite
vincentmr Feb 16, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27,589 changes: 27,589 additions & 0 deletions .github/workflows/core_tests_durations.json

Large diffs are not rendered by default.

18 changes: 8 additions & 10 deletions .github/workflows/interface-unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,20 @@ jobs:
cat >matrix_max_parallel.json <<-EOF
{
"default": 1,
"tf-tests": 3,
"core-tests": 3,
"core-tests": 5,
vincentmr marked this conversation as resolved.
Show resolved Hide resolved
"gradients-tests": 2,
"jax-tests": 5,
"gradients-tests": 2
"tf-tests": 3
}
EOF
else
cat >matrix_max_parallel.json <<-EOF
{
"default": 1,
"torch-tests": 2,
"tf-tests": 6,
"core-tests": 5,
"jax-tests": 10,
"core-tests": 3
"tf-tests": 6,
"torch-tests": 2
}
EOF
fi
Expand Down Expand Up @@ -264,9 +264,7 @@ jobs:
|| fromJSON(needs.setup-ci-load.outputs.matrix-max-parallel).default
}}
matrix:
group:
- '1'
- '2'
group: [1, 2, 3, 4, 5]
python-version: >-
${{
fromJSON(needs.setup-ci-load.outputs.python-version).core-tests
Expand All @@ -286,7 +284,7 @@ jobs:
install_pennylane_lightning_master: true
pytest_coverage_flags: ${{ inputs.pytest_coverage_flags }}
pytest_markers: core and not qcut and not finite-diff and not param-shift
pytest_additional_args: --splits 2 --group ${{ matrix.group }}
pytest_additional_args: --splits 5 --group ${{ matrix.group }} --durations-path='.github/workflows/core_tests_durations.json'
additional_pip_packages: pytest-split
requirements_file: ${{ strategy.job-index == 0 && 'core.txt' || '' }}

Expand Down
61 changes: 35 additions & 26 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@
```pycon
>>> circuit()
array([[0, 1, 1, 0, 0],
[1, 0, 1, 1, 1],
[0, 0, 0, 1, 0],
[1, 0, 0, 1, 1]])
[1, 0, 1, 1, 1],
[0, 0, 0, 1, 0],
[1, 0, 0, 1, 1]])
```

* A function called `apply_operation` has been added to the new `qutrit_mixed` module found in `qml.devices` that applies operations to device-compatible states.
Expand All @@ -146,7 +146,7 @@
* PennyLane can now use lightning provided VJPs by selecting `device_vjp=True` on the QNode.
[(#4914)](https://github.com/PennyLaneAI/pennylane/pull/4914)

* Remove queuing (`AnnotatedQueue`) from `qml.cut_circuit` and `qml.cut_circuit_mc` to improve performance
* Remove queuing (`AnnotatedQueue`) from `qml.cut_circuit` and `qml.cut_circuit_mc` to improve performance
for large workflows.
[(#5108)](https://github.com/PennyLaneAI/pennylane/pull/5108)

Expand All @@ -156,27 +156,35 @@
<h4>Community contributions 🥳</h4>

* `parity_transform` is added for parity mapping of a fermionic Hamiltonian.
[(#4928)](https://github.com/PennyLaneAI/pennylane/pull/4928)
It is now possible to transform a fermionic Hamiltonian to a qubit Hamiltonian with parity mapping.
[(#4928)](https://github.com/PennyLaneAI/pennylane/pull/4928)
It is now possible to transform a fermionic Hamiltonian to a qubit Hamiltonian with parity mapping.

```python
import pennylane as qml
fermi_ham = qml.fermi.FermiWord({(0, 0) : '+', (1, 1) : '-'})
```python
import pennylane as qml
fermi_ham = qml.fermi.FermiWord({(0, 0) : '+', (1, 1) : '-'})

qubit_ham = qml.fermi.parity_transform(fermi_ham, n=6)
```

```pycon
>>> print(qubit_ham)
(-0.25j*(PauliY(wires=[0]))) + ((-0.25+0j)*(PauliX(wires=[0]) @ PauliZ(wires=[1]))) +
((0.25+0j)*(PauliX(wires=[0]))) + (0.25j*(PauliY(wires=[0]) @ PauliZ(wires=[1])))
```

qubit_ham = qml.fermi.parity_transform(fermi_ham, n=6)
```
<h3>Improvements 🛠</h3>

```pycon
>>> print(qubit_ham)
(-0.25j*(PauliY(wires=[0]))) + ((-0.25+0j)*(PauliX(wires=[0]) @ PauliZ(wires=[1]))) +
((0.25+0j)*(PauliX(wires=[0]))) + (0.25j*(PauliY(wires=[0]) @ PauliZ(wires=[1])))
```
* The `default.qubit` device now supports mid-circuit measurements without using the deferred measurement
principle when using finite shots. The native execution mode comprises all features supported with `qml.defer_measurements`, including
classical control, collecting statistics, and post-selection. This PR notably introduces support for
post-selection, measurement value lists, and composite measurements.
[(#5120)](https://github.com/PennyLaneAI/pennylane/pull/5120)

* The transform `split_non_commuting` now accepts measurements of type `probs`, `sample` and `counts` which accept both wires and observables.
[(#4972)](https://github.com/PennyLaneAI/pennylane/pull/4972)

* Improve efficiency of matrix calculation when operator is symmetric over wires
[(#3601)](https://github.com/PennyLaneAI/pennylane/pull/3601)
[(#3601)](https://github.com/PennyLaneAI/pennylane/pull/3601)

* The module `pennylane/math/quantum.py` has now support for the min-entropy.
[(#3959)](https://github.com/PennyLaneAI/pennylane/pull/3959/)
Expand Down Expand Up @@ -342,13 +350,14 @@
(with potentially negative eigenvalues) has been implemented.
[(#5048)](https://github.com/PennyLaneAI/pennylane/pull/5048)

* Controlled operators with a custom controlled version decomposes like how their
controlled counterpart decomposes, as opposed to decomposing into their controlled version.
* Controlled operators with a custom controlled version decomposes like how their controlled
counterpart decomposes, as opposed to decomposing into their controlled version.
[(#5069)](https://github.com/PennyLaneAI/pennylane/pull/5069)
[(#5125)](https://github.com/PennyLaneAI/pennylane/pull/5125/)

For example:
```

```pycon
>>> qml.ctrl(qml.RX(0.123, wires=1), control=0).decomposition()
[
RZ(1.5707963267948966, wires=[1]),
Expand Down Expand Up @@ -486,16 +495,16 @@
* `PauliSentence.wires` no longer imposes a false order.
[(#5041)](https://github.com/PennyLaneAI/pennylane/pull/5041)

* `qml.qchem.import_state` now applies the chemist-to-physicist
* `qml.qchem.import_state` now applies the chemist-to-physicist
sign convention when initializing a PennyLane state vector from
classically pre-computed wavefunctions. That is, it interleaves
classically pre-computed wavefunctions. That is, it interleaves
spin-up/spin-down operators for the same spatial orbital index,
as standard in PennyLane (instead of commuting all spin-up
operators to the left, as is standard in quantum chemistry).
as standard in PennyLane (instead of commuting all spin-up
operators to the left, as is standard in quantum chemistry).
[(#5114)](https://github.com/PennyLaneAI/pennylane/pull/5114)

* Multi-wire controlled `CNOT` and `PhaseShift` can now be decomposed correctly.
[(#5125)](https://github.com/PennyLaneAI/pennylane/pull/5125/)
[(#5125)](https://github.com/PennyLaneAI/pennylane/pull/5125/)
[(#5148)](https://github.com/PennyLaneAI/pennylane/pull/5148)

* `draw_mpl` no longer raises an error when drawing a circuit containing an adjoint of a controlled operation.
Expand Down
9 changes: 6 additions & 3 deletions pennylane/devices/qubit/apply_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def apply_conditional(
Returns:
ndarray: output state
"""
if mid_measurements[op.meas_val.measurements[0]]:
if op.meas_val.concretize(mid_measurements):
return apply_operation(
op.then_op,
state,
Expand Down Expand Up @@ -260,17 +260,20 @@ def apply_mid_measure(
"""
if is_state_batched:
raise ValueError("MidMeasureMP cannot be applied to batched states.")
if not np.allclose(np.linalg.norm(state), 1.0):
vincentmr marked this conversation as resolved.
Show resolved Hide resolved
mid_measurements[op] = 0
return np.zeros_like(state)
wire = op.wires
probs = qml.devices.qubit.measure(qml.probs(wire), state)
sample = np.random.binomial(1, probs[1])
mid_measurements[op] = sample
if op.postselect is not None and sample != op.postselect:
return np.zeros_like(state)
axis = wire.toarray()[0]
slices = [slice(None)] * qml.math.ndim(state)
slices[axis] = int(not sample)
state[tuple(slices)] = 0.0
state_norm = np.linalg.norm(state)
if state_norm < 1.0e-15:
raise ValueError("Cannot normalize projected state.")
state = state / state_norm
if op.reset and sample == 1:
state = apply_operation(
Expand Down
Loading
Loading