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

Circuit cutting: Add input validation checks #2251

Merged
merged 9 commits into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
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
1 change: 1 addition & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
A suite of integration tests has been added.
[(#2231)](https://github.com/PennyLaneAI/pennylane/pull/2231)
[(#2234)](https://github.com/PennyLaneAI/pennylane/pull/2234)
[(#2251)](https://github.com/PennyLaneAI/pennylane/pull/2251)

<h3>Improvements</h3>

Expand Down
28 changes: 27 additions & 1 deletion pennylane/transforms/qcut.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
from pennylane.operation import Expectation, Operation, Operator, Tensor
from pennylane.ops.qubit.non_parametric_ops import WireCut
from pennylane.tape import QuantumTape
from pennylane.transforms import batch_transform
from pennylane.wires import Wires

from .batch_transform import batch_transform
Expand Down Expand Up @@ -941,6 +940,33 @@ def circuit(x):
>>> qml.grad(cut_circuit)(x)
-0.506395895364911
"""
if len(tape.measurements) != 1:
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we want to exclude tapes without measurements? Would be fairly simple to return fragment tapes. Though use cases for this is questionable

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it makes sense to exclude the zero-measurements case:

  • It seems strange to go through the (potentially huge) process of cut placement and fragment execution, to contract a tensor network to nothing
  • I'm also not sure if it works in the current pipeline in practice
  • This is anyway quite an edge case because QNodes will always have at least one measurement. So only if a user applies the transform to a tape could they see this warning.
  • It's assumed in this PR

raise ValueError(
"The circuit cutting workflow only supports circuits with a single output "
"measurement"
)

if not all(m.return_type is Expectation for m in tape.measurements):
raise ValueError(
"The circuit cutting workflow only supports circuits with expectation "
"value measurements"
)

if use_opt_einsum:
try:
import opt_einsum # pylint: disable=import-outside-toplevel,unused-import
except ImportError as e:
raise ImportError(
"The opt_einsum package is required when use_opt_einsum is set to "
"True in the cut_circuit function. This package can be "
"installed using:\npip install opt_einsum"
) from e

num_cut = len([op for op in tape.operations if isinstance(op, WireCut)])
if num_cut == 0:
raise ValueError(
"Cannot apply the circuit cutting workflow to a circuit without any cuts"
)

g = tape_to_graph(tape)
replace_wire_cut_nodes(g)
Expand Down
51 changes: 51 additions & 0 deletions tests/transforms/test_qcut.py
Original file line number Diff line number Diff line change
Expand Up @@ -2106,6 +2106,57 @@ def circuit(x):
assert np.isclose(grad, grad_expected)


class TestCutCircuitTransformValidation:
"""Tests of validation checks in the cut_circuit function"""

def test_multiple_measurements_raises(self):
"""Tests if a ValueError is raised when a tape with multiple measurements is requested
to be cut"""

with qml.tape.QuantumTape() as tape:
qml.expval(qml.PauliZ(0))
qml.expval(qml.PauliZ(1))

with pytest.raises(ValueError, match="The circuit cutting workflow only supports circuits"):
qcut.cut_circuit(tape)

def test_no_measurements_raises(self):
"""Tests if a ValueError is raised when a tape with no measurement is requested
to be cut"""
with pytest.raises(ValueError, match="The circuit cutting workflow only supports circuits"):
qcut.cut_circuit(qml.tape.QuantumTape())

def test_non_expectation_raises(self):
"""Tests if a ValueError is raised when a tape with measurements that are not expectation
values is requested to be cut"""

with qml.tape.QuantumTape() as tape:
qml.var(qml.PauliZ(0))

with pytest.raises(ValueError, match="workflow only supports circuits with expectation"):
qcut.cut_circuit(tape)

def test_fail_import(self, monkeypatch):
"""Test if an ImportError is raised when opt_einsum is requested but not installed"""
with qml.tape.QuantumTape() as tape:
qml.expval(qml.PauliZ(0))

with monkeypatch.context() as m:
m.setitem(sys.modules, "opt_einsum", None)

with pytest.raises(ImportError, match="The opt_einsum package is required"):
qcut.cut_circuit(tape, use_opt_einsum=True)

def test_no_cuts_raises(self):
"""Tests if a ValueError is raised when circuit cutting is to be applied to a circuit
without cuts"""
with qml.tape.QuantumTape() as tape:
qml.expval(qml.PauliZ(0))

with pytest.raises(ValueError, match="to a circuit without any cuts"):
qcut.cut_circuit(tape)


class TestCutStrategy:
"""Tests for class CutStrategy"""

Expand Down