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

Integration testing for the cut_circuit_mc transform #2407

Merged
merged 75 commits into from
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
248f83a
extend graph_to_tape for sampling, add unit test
anthayes92 Mar 11, 2022
7b07eab
add PR to changelog
anthayes92 Mar 12, 2022
b9f65e2
add fragment sample circuit unit test
anthayes92 Mar 12, 2022
d44e63a
Apply suggestions from code review
anthayes92 Mar 14, 2022
c6ac586
add sampling tensor obs error, add test
anthayes92 Mar 14, 2022
5a4f9e7
add test for sample obs
anthayes92 Mar 14, 2022
cf33690
Apply suggestions from code review
anthayes92 Mar 14, 2022
61c32fc
extend graph to tape logic to sample measurements, add unit test
anthayes92 Mar 14, 2022
fcf2c3e
remove unused logic, add mid circuit meas test
anthayes92 Mar 14, 2022
efbc343
update changelog
anthayes92 Mar 14, 2022
4b65562
Merge branch 'master' into qcut-sample-subgraphs-to-fragment-tape-con…
anthayes92 Mar 14, 2022
7983ba0
fix variables
anthayes92 Mar 15, 2022
5ab21b6
Merge branch 'qcut-sample-subgraphs-to-fragment-tape-conversion' of g…
anthayes92 Mar 15, 2022
81c71f7
Apply suggestions from code review
anthayes92 Mar 15, 2022
e8971b9
add suggestions from code review
anthayes92 Mar 15, 2022
201d400
add upsupported meas exception, add unit test
anthayes92 Mar 15, 2022
ad1c479
fix codefactor issue
anthayes92 Mar 15, 2022
71ceb83
Merge branch 'master' into qcut-sample-subgraphs-to-fragment-tape-con…
anthayes92 Mar 15, 2022
9ed788d
add expand_fragment_tapes_mc function and unit test
anthayes92 Mar 15, 2022
70bc71f
update docs
anthayes92 Mar 16, 2022
4237654
update changelog
anthayes92 Mar 16, 2022
b3866d1
update unit test
anthayes92 Mar 16, 2022
6c1c17b
add unit tests for MC measurements and state preps
anthayes92 Mar 17, 2022
a71dc9e
update expansion logic, add unit test
anthayes92 Mar 17, 2022
558cc79
fix wire bug
anthayes92 Mar 17, 2022
1d793c9
Apply suggestions from code review
anthayes92 Mar 18, 2022
c609ded
Merge branch 'master' into qcut-sample-fragment-configurations
anthayes92 Mar 18, 2022
f9b8ddf
add suggestions from code review
anthayes92 Mar 18, 2022
65a9226
begin mc postprocessing function, add unit test
anthayes92 Mar 22, 2022
86010d4
attempt MC postprocessing
anthayes92 Mar 22, 2022
fa8ef15
update sample postprocessing, add mc, add unit test
anthayes92 Mar 22, 2022
ab910a5
Merge branch 'master' into qcut-sampling-postprocessing-function
anthayes92 Mar 22, 2022
4f659ba
add mc unit test, update docs and changlog
anthayes92 Mar 23, 2022
796e9ba
Merge branch 'qcut-sampling-postprocessing-function' of github.com:Pe…
anthayes92 Mar 23, 2022
0206cec
Merge branch 'master' into qcut-sampling-postprocessing-function
anthayes92 Mar 23, 2022
b912475
Apply suggestions from code review
anthayes92 Mar 23, 2022
d00b742
add suggestions from code review
anthayes92 Mar 23, 2022
1deb4cc
Merge branch 'master' into qcut-sampling-postprocessing-function
trbromley Mar 24, 2022
09cc5f2
add remaining suggestions
anthayes92 Mar 24, 2022
18edb8f
add cut_circuit_mc transform, add unit test
anthayes92 Mar 24, 2022
3dee6ac
update docs, changelog, tests
anthayes92 Mar 25, 2022
f1fbc87
Apply suggestions from code review
anthayes92 Mar 28, 2022
62988e4
add suggestions from code review
anthayes92 Mar 28, 2022
dde7fdd
update shots logic, add unit tests
anthayes92 Mar 28, 2022
a85c1fd
draft usage details
anthayes92 Mar 29, 2022
99164bb
Merge branch 'master' into qcut-cut-circuit-mc-transform
anthayes92 Mar 29, 2022
42c646f
add validation, add unit tests
anthayes92 Mar 29, 2022
da58701
update usage details wording
anthayes92 Mar 30, 2022
3798113
update _reshape_results helper function and unit test
anthayes92 Mar 30, 2022
74564b9
update usage details with working example
anthayes92 Mar 30, 2022
f5a3369
Apply suggestions from code review
anthayes92 Mar 30, 2022
e16c864
add review suggestions
anthayes92 Mar 30, 2022
46266d4
add unit tests for validation
anthayes92 Mar 30, 2022
fbadb4f
Merge branch 'master' into qcut-cut-circuit-mc-transform
anthayes92 Mar 30, 2022
eb0d5af
fix test
anthayes92 Mar 30, 2022
8956720
Merge branch 'qcut-cut-circuit-mc-transform' of github.com:PennyLaneA…
anthayes92 Mar 30, 2022
22c6013
add _qcut_expand_fn_mc and unit tests
anthayes92 Mar 31, 2022
7f320d8
update changelog
anthayes92 Mar 31, 2022
e36d53f
retrigger checks
anthayes92 Apr 1, 2022
88bdca4
revert merge conflict
anthayes92 Apr 1, 2022
932eb2e
add interface test
anthayes92 Apr 1, 2022
a57f31a
add more tests
anthayes92 Apr 1, 2022
6a4571f
add complicated circuit test
anthayes92 Apr 5, 2022
64c6fe5
fix and update unit tests
anthayes92 Apr 6, 2022
7742379
update changelog
anthayes92 Apr 6, 2022
91b6fa7
Merge branch 'master' into qcut-extend-mc-cutting-tests
trbromley Apr 7, 2022
2279529
Apply suggestions from code review
anthayes92 Apr 7, 2022
a789065
Merge branch 'master' into qcut-extend-mc-cutting-tests
anthayes92 Apr 8, 2022
c0260a1
update unit test
anthayes92 Apr 8, 2022
a1adee8
update cut_circuit_mc args
anthayes92 Apr 8, 2022
46eaa13
fix defaults
anthayes92 Apr 8, 2022
7021520
Merge branch 'master' into qcut-extend-mc-cutting-tests
anthayes92 Apr 8, 2022
707fb44
retrigger checks
anthayes92 Apr 8, 2022
a9b81a3
Merge branch 'qcut-extend-mc-cutting-tests' of github.com:PennyLaneAI…
anthayes92 Apr 8, 2022
1ffe8da
QCut sample-based circuit cutting changleog update (#2424)
anthayes92 Apr 8, 2022
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
131 changes: 101 additions & 30 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,44 +104,115 @@
- The dependency on openbabel is removed
[(#2415)](https://github.com/PennyLaneAI/pennylane/pull/2415)

* Development of a circuit-cutting compiler extension to circuits with sampling
measurements has begun:

- The existing `qcut.tape_to_graph()` method has been extended to convert a
sample measurement without an observable specified to multiple single-qubit sample
nodes.
* <h4> Finite-shot circuit cutting ✂️</h4>

* You can now run `N`-wire circuits containing sample-based measurements on
devices with fewer than `N` wires by inserting `WireCut` operations into
the circuit and decorating your QNode with `@qml.cut_circuit_mc`.
With this, samples from the original circuit can be simulated using
a Monte Carlo method,
using fewer qubits at the expense of more device executions. Additionally,
this transform
can take an optional classical processing function as an argument
and return an expectation value.
[(#2313)](https://github.com/PennyLaneAI/pennylane/pull/2313)
[(#2321)](https://github.com/PennyLaneAI/pennylane/pull/2321)
[(#2332)](https://github.com/PennyLaneAI/pennylane/pull/2332)
[(#2358)](https://github.com/PennyLaneAI/pennylane/pull/2358)
[(#2382)](https://github.com/PennyLaneAI/pennylane/pull/2382)
[(#2399)](https://github.com/PennyLaneAI/pennylane/pull/2399)
[(#2407)](https://github.com/PennyLaneAI/pennylane/pull/2407)

The following `3`-qubit circuit contains a `WireCut` operation and a `sample`
measurement. When decorated with `@qml.cut_circuit_mc`, we can cut the circuit
into two `2`-qubit fragments:

```python

dev = qml.device("default.qubit", wires=2, shots=1000)

@qml.cut_circuit_mc
@qml.qnode(dev)
def circuit(x):
qml.RX(0.89, wires=0)
qml.RY(0.5, wires=1)
qml.RX(1.3, wires=2)

qml.CNOT(wires=[0, 1])
qml.WireCut(wires=1)
qml.CNOT(wires=[1, 2])

qml.RX(x, wires=0)
qml.RY(0.7, wires=1)
qml.RX(2.3, wires=2)
return qml.sample(wires=[0, 2])
```

we can then execute the circuit as usual by calling the QNode:

```pycon
>>> x = 0.3
>>> circuit(x)
tensor([[1, 1],
[0, 1],
[0, 1],
...,
[0, 1],
[0, 1],
[0, 1]], requires_grad=True)
```

Furthermore, the number of shots can be temporarily altered when calling
the QNode:

```pycon
>>> results = circuit(x, shots=123)
>>> results.shape
(123, 2)
```

Using the Monte Carlo approach of [Peng et. al](https://arxiv.org/abs/1904.00102), the
`cut_circuit_mc` transform also supports returning sample-based expectation values of
observables that are diagonal in the computational basis, as shown below for a `ZZ` measurement
on wires `0` and `2`:

```python
dev = qml.device("default.qubit", wires=2, shots=10000)

def observable(bitstring):
return (-1) ** np.sum(bitstring)

@qml.cut_circuit_mc(classical_processing_fn=observable)
@qml.qnode(dev)
def circuit(x):
qml.RX(0.89, wires=0)
qml.RY(0.5, wires=1)
qml.RX(1.3, wires=2)

qml.CNOT(wires=[0, 1])
qml.WireCut(wires=1)
qml.CNOT(wires=[1, 2])

qml.RX(x, wires=0)
qml.RY(0.7, wires=1)
qml.RX(2.3, wires=2)
return qml.sample(wires=[0, 2])
```

We can now approximate the expectation value of the observable using

```pycon
>>> circuit(x)
tensor(-0.776, requires_grad=True)
```

- An automatic graph partitioning method `qcut.kahypar_cut()` has been implemented for cutting
arbitrary tape-converted graphs using the general purpose graph partitioning framework
[KaHyPar](https://pypi.org/project/kahypar/) which needs to be installed separately.
To integrate with the existing manual cut pipeline, method `qcut.find_and_place_cuts()` and related
utilities are implemented which uses `qcut.kahypar_cut()` as the default auto cutter.
[(#2330)](https://github.com/PennyLaneAI/pennylane/pull/2330)

- The existing `qcut.graph_to_tape()` method has been extended to convert
graphs containing sample measurement nodes to tapes.
[(#2321)](https://github.com/PennyLaneAI/pennylane/pull/2321)

- A `qcut.expand_fragment_tapes_mc()` method has been added to expand fragment
tapes to random configurations by replacing measure and prepare nodes with
sampled Pauli measurements and state preparations.
[(#2332)](https://github.com/PennyLaneAI/pennylane/pull/2332)

- Postprocessing functions `qcut.qcut_processing_fn_sample()` and
`qcut.qcut_processing_fn_mc()` have been added to return samples and expectation
values, respectively, of recombined fragments using the Monte Carlo sampling
approach.
[(#2358)](https://github.com/PennyLaneAI/pennylane/pull/2358)

- A user-facing transform for circuit cutting with sample measurements has
been added. A `qnode` containing `WireCut` operations and `sample` measurements
can be decorated with `@qml.cut_circuit_mc()` to perform this type of cutting.
[(#2382)](https://github.com/PennyLaneAI/pennylane/pull/2382)

- Add expansion to `qcut.cut_circuit_mc()` to search for wire cuts in
contained operations or tapes.
[(#2399)](https://github.com/PennyLaneAI/pennylane/pull/2399)

<h3>Improvements</h3>

* The parameter-shift Hessian can now be computed for arbitrary
Expand Down
8 changes: 4 additions & 4 deletions pennylane/transforms/qcut.py
Original file line number Diff line number Diff line change
Expand Up @@ -946,10 +946,10 @@ def qcut_processing_fn_mc(
@batch_transform
def cut_circuit_mc(
tape: QuantumTape,
shots: Optional[int] = None,
device_wires: Optional[Wires] = None,
classical_processing_fn: Optional[callable] = None,
max_depth: int = 1,
shots: Optional[int] = None,
device_wires: Optional[Wires] = None,
) -> Tuple[Tuple[QuantumTape], Callable]:
"""
Cut up a circuit containing sample measurements into smaller fragments using a
Expand Down Expand Up @@ -1930,10 +1930,10 @@ def _cut_circuit_expand(

def _cut_circuit_mc_expand(
tape: QuantumTape,
shots: Optional[int] = None,
device_wires: Optional[Wires] = None,
classical_processing_fn: Optional[callable] = None,
max_depth: int = 1,
shots: Optional[int] = None,
device_wires: Optional[Wires] = None,
):
"""Main entry point for expanding operations in sample-based tapes until
reaching a depth that includes :class:`~.WireCut` operations."""
Expand Down
Loading