Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 0 additions & 6 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@
* Added ``catalyst.switch``, a qjit compatible, index-switch style control flow decorator.
[(#2171)](https://github.com/PennyLaneAI/catalyst/pull/2171)

* Catalyst can now compile circuits that are directly expressed in terms of Pauli product rotation
(PPR) and Pauli product measurement (PPM) operations: :class:`~.PauliRot` and
:func:`~.pauli_measure`, respectively. This support enables research and development
spurred from `A Game of Surface Codes (arXiv1808.02892) <https://arxiv.org/pdf/1808.02892>`_.
[(#2145)](https://github.com/PennyLaneAI/catalyst/pull/2145)

:class:`~.PauliRot` and :func:`~.pauli_measure` can be manipulated with Catalyst's existing passes
for PPR-PPM compilation, which includes :func:`catalyst.passes.to_ppr`,
:func:`catalyst.passes.commute_ppr`, :func:`catalyst.passes.merge_ppr_ppm`,
Expand Down
58 changes: 45 additions & 13 deletions frontend/catalyst/device/qjit_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,44 @@
logger = logging.getLogger(__name__)
logger.addHandler(logging.NullHandler())

RUNTIME_OPERATIONS = [
"CNOT",
"ControlledPhaseShift",
"CRot",
"CRX",
"CRY",
"CRZ",
"CSWAP",
"CY",
"CZ",
"Hadamard",
"Identity",
"IsingXX",
"IsingXY",
"IsingYY",
"IsingZZ",
"SingleExcitation",
"DoubleExcitation",
"ISWAP",
"MultiRZ",
"PauliX",
"PauliY",
"PauliZ",
"PCPhase",
"PhaseShift",
"PSWAP",
"QubitUnitary",
"Rot",
"RX",
"RY",
"RZ",
"S",
"SWAP",
"T",
"Toffoli",
"GlobalPhase",
]

RUNTIME_OBSERVABLES = [
"Identity",
"PauliX",
Expand All @@ -71,9 +109,11 @@

RUNTIME_MPS = ["ExpectationMP", "SampleMP", "VarianceMP", "CountsMP", "StateMP", "ProbabilityMP"]

# A list of custom operations supported by the Catalyst compiler.
# This is useful especially for testing a device with custom operations.
CUSTOM_OPERATIONS = {}
# The runtime interface does not care about specific gate properties, so set them all to True.
RUNTIME_OPERATIONS = {
op: OperatorProperties(invertible=True, controllable=True, differentiable=True)
for op in RUNTIME_OPERATIONS
}

RUNTIME_OBSERVABLES = {
obs: OperatorProperties(invertible=True, controllable=True, differentiable=True)
Expand Down Expand Up @@ -159,14 +199,6 @@ def extract_backend_info(device: qml.devices.QubitDevice) -> BackendInfo:
return BackendInfo(dname, device_name, device_lpath, device_kwargs)


def union_operations(
a: Dict[str, OperatorProperties], b: Dict[str, OperatorProperties]
) -> Dict[str, OperatorProperties]:
"""Union of two sets of operator properties"""
return {**a, **b}
# return {k: a[k] & b[k] for k in (a.keys() & b.keys())}


def intersect_operations(
a: Dict[str, OperatorProperties], b: Dict[str, OperatorProperties]
) -> Dict[str, OperatorProperties]:
Expand All @@ -191,8 +223,8 @@ def get_qjit_device_capabilities(target_capabilities: DeviceCapabilities) -> Dev
qjit_capabilities = deepcopy(target_capabilities)

# Intersection of gates and observables supported by the device and by Catalyst runtime.
qjit_capabilities.operations = union_operations(
target_capabilities.operations, CUSTOM_OPERATIONS
qjit_capabilities.operations = intersect_operations(
target_capabilities.operations, RUNTIME_OPERATIONS
)
qjit_capabilities.observables = intersect_operations(
target_capabilities.observables, RUNTIME_OBSERVABLES
Expand Down
8 changes: 4 additions & 4 deletions frontend/test/pytest/test_preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,14 @@ def test_decompose_integration(self):

@qml.qnode(dev)
def circuit(theta: float):
qml.OrbitalRotation(theta, wires=[0, 1, 2, 3])
qml.SingleExcitationPlus(theta, wires=[0, 1])
return qml.state()

mlir = qjit(circuit, target="mlir").mlir
assert "SingleExcitation" in mlir
assert "Hadamard" in mlir
assert "RX" in mlir
assert "OrbitalRotation" not in mlir
assert "CNOT" in mlir
assert "RY" in mlir
assert "SingleExcitationPlus" not in mlir

def test_decompose_ops_to_unitary(self):
"""Test the decompose ops to unitary transform."""
Expand Down
22 changes: 11 additions & 11 deletions frontend/test/pytest/test_verification.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from catalyst.api_extensions import HybridAdjoint, HybridCtrl
from catalyst.compiler import get_lib_path
from catalyst.device import get_device_capabilities
from catalyst.device.qjit_device import CUSTOM_OPERATIONS, get_qjit_device_capabilities
from catalyst.device.qjit_device import RUNTIME_OPERATIONS, get_qjit_device_capabilities
from catalyst.device.verification import validate_measurements

# pylint: disable = unused-argument, unnecessary-lambda-assignment, unnecessary-lambda
Expand Down Expand Up @@ -290,7 +290,7 @@ def test_non_controllable_gate_hybridctrl(self):
# Note: The HybridCtrl operator is not currently supported with the QJIT device, but the
# verification structure is in place, so we test the verification of its nested operators by
# adding HybridCtrl to the list of native gates for the custom base device and by patching
# the list of CUSTOM_OPERATIONS for the QJIT device to include HybridCtrl for this test.
# the list of RUNTIME_OPERATIONS for the QJIT device to include HybridCtrl for this test.

@qml.qnode(
get_custom_device(
Expand All @@ -302,12 +302,12 @@ def f(x: float):
assert isinstance(op, HybridCtrl), f"op expected to be HybridCtrl but got {type(op)}"
return qml.expval(qml.PauliX(0))

runtime_ops_with_qctrl = deepcopy(CUSTOM_OPERATIONS)
runtime_ops_with_qctrl = deepcopy(RUNTIME_OPERATIONS)
runtime_ops_with_qctrl["HybridCtrl"] = OperatorProperties(
invertible=True, controllable=True, differentiable=True
)

with patch("catalyst.device.qjit_device.CUSTOM_OPERATIONS", runtime_ops_with_qctrl):
with patch("catalyst.device.qjit_device.RUNTIME_OPERATIONS", runtime_ops_with_qctrl):
with pytest.raises(CompileError, match="PauliZ is not controllable"):
qjit(f)(1.2)

Expand All @@ -321,7 +321,7 @@ def test_hybridctrl_raises_error(self):
"""Test that a HybridCtrl operator is rejected by the verification."""

# TODO: If you are deleting this test because HybridCtrl support has been added, consider
# updating the tests that patch CUSTOM_OPERATIONS to inclue HybridCtrl accordingly
# updating the tests that patch RUNTIME_OPERATIONS to inclue HybridCtrl accordingly

@qml.qnode(get_custom_device(non_controllable_gates={"PauliZ"}, wires=4))
def f(x: float):
Expand Down Expand Up @@ -391,7 +391,7 @@ def test_hybrid_ctrl_containing_adjoint(self, adjoint_type, unsupported_gate_att
# Note: The HybridCtrl operator is not currently supported with the QJIT device, but the
# verification structure is in place, so we test the verification of its nested operators by
# adding HybridCtrl to the list of native gates for the custom base device and by patching
# the list of CUSTOM_OPERATIONS for the QJIT device to include HybridCtrl for this test.
# the list of RUNTIME_OPERATIONS for the QJIT device to include HybridCtrl for this test.

def _ops(x, wires):
if adjoint_type == HybridAdjoint:
Expand All @@ -410,12 +410,12 @@ def f(x: float):
assert isinstance(base, adjoint_type), f"expected {adjoint_type} but got {type(op)}"
return qml.expval(qml.PauliX(0))

runtime_ops_with_qctrl = deepcopy(CUSTOM_OPERATIONS)
runtime_ops_with_qctrl = deepcopy(RUNTIME_OPERATIONS)
runtime_ops_with_qctrl["HybridCtrl"] = OperatorProperties(
invertible=True, controllable=True, differentiable=True
)

with patch("catalyst.device.qjit_device.CUSTOM_OPERATIONS", runtime_ops_with_qctrl):
with patch("catalyst.device.qjit_device.RUNTIME_OPERATIONS", runtime_ops_with_qctrl):
with pytest.raises(CompileError, match=f"PauliZ is not {unsupported_gate_attribute}"):
qjit(f)(1.2)

Expand All @@ -434,7 +434,7 @@ def test_hybrid_adjoint_containing_hybrid_ctrl(self, ctrl_type, unsupported_gate
# Note: The HybridCtrl operator is not currently supported with the QJIT device, but the
# verification structure is in place, so we test the verification of its nested operators by
# adding HybridCtrl to the list of native gates for the custom base device and by patching
# the list of CUSTOM_OPERATIONS for the QJIT device to include HybridCtrl for this test.
# the list of RUNTIME_OPERATIONS for the QJIT device to include HybridCtrl for this test.

def _ops(x, wires):
if ctrl_type == HybridCtrl:
Expand All @@ -453,12 +453,12 @@ def f(x: float):
assert isinstance(base, ctrl_type), f"expected {ctrl_type} but got {type(op)}"
return qml.expval(qml.PauliX(0))

runtime_ops_with_qctrl = deepcopy(CUSTOM_OPERATIONS)
runtime_ops_with_qctrl = deepcopy(RUNTIME_OPERATIONS)
runtime_ops_with_qctrl["HybridCtrl"] = OperatorProperties(
invertible=True, controllable=True, differentiable=True
)

with patch("catalyst.device.qjit_device.CUSTOM_OPERATIONS", runtime_ops_with_qctrl):
with patch("catalyst.device.qjit_device.RUNTIME_OPERATIONS", runtime_ops_with_qctrl):
with pytest.raises(CompileError, match=f"PauliZ is not {unsupported_gate_attribute}"):
qjit(f)(1.2)

Expand Down
Loading