Skip to content

Commit

Permalink
Remove obsolete TOML code (#785)
Browse files Browse the repository at this point in the history
This PR aims to remove obsolete TOML related code and also resolve
incompatibilities with the latest lightning device versions.

- The verifier is relaxed to only test agreement of basic gates (no
adjoint/control) between the toml and python device spec.
- This is needed to resolve the overlapping gate issue introduced by
PennyLaneAI/pennylane-lightning#743
- and also allows removing the patch for Lightning Kokkos that exists
solely to satisfy the verifier.

- The obsolete toml files in the catalyst repo for lightning devices is
also removed.

- MCMs are marked as un-invertible and un-controllable, since they are
non-unitary.

Requires: PennyLaneAI/pennylane-lightning#758

[sc-65114]
  • Loading branch information
dime10 authored Jun 18, 2024
1 parent 422c609 commit 5612227
Show file tree
Hide file tree
Showing 13 changed files with 32 additions and 292 deletions.
1 change: 1 addition & 0 deletions .dep-versions
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ enzyme=1beb98b51442d50652eaa3ffb9574f4720d611f1

# Always remove custom PL/LQ versions before release.
pennylane=9b3060fd56e1e65a0d69a27b8a13583ce716a538
lightning=0.37.0-dev42
2 changes: 1 addition & 1 deletion .github/workflows/build-wheel-linux-x86_64.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ jobs:
-Dpybind11_DIR=$(python${{ matrix.python_version }} -c "import pybind11; print(pybind11.get_cmake_dir())") \
-DENABLE_LIGHTNING_KOKKOS=ON \
-DENABLE_LAPACK=OFF \
-DLIGHTNING_GIT_TAG=latest_release \
-DLIGHTNING_GIT_TAG=476cac2e39f79c122b8751a77aad8139b39296db \
-DKokkos_ENABLE_SERIAL=ON \
-DKokkos_ENABLE_OPENMP=ON \
-DENABLE_WARNINGS=OFF \
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build-wheel-macos-arm64.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ jobs:
-DPYTHON_EXECUTABLE=$(which python${{ matrix.python_version }}) \
-Dpybind11_DIR=$(python${{ matrix.python_version }} -c "import pybind11; print(pybind11.get_cmake_dir())") \
-DENABLE_LIGHTNING_KOKKOS=ON \
-DLIGHTNING_GIT_TAG=latest_release \
-DLIGHTNING_GIT_TAG=476cac2e39f79c122b8751a77aad8139b39296db \
-DENABLE_LAPACK=OFF \
-DKokkos_ENABLE_SERIAL=ON \
-DKokkos_ENABLE_OPENMP=ON \
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build-wheel-macos-x86_64.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ jobs:
-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=$GITHUB_WORKSPACE/runtime-build/lib \
-DPYTHON_EXECUTABLE=$(which python${{ matrix.python_version }}) \
-Dpybind11_DIR=$(python${{ matrix.python_version }} -c "import pybind11; print(pybind11.get_cmake_dir())") \
-DLIGHTNING_GIT_TAG=latest_release \
-DLIGHTNING_GIT_TAG=476cac2e39f79c122b8751a77aad8139b39296db \
-DENABLE_LIGHTNING_KOKKOS=ON \
-DENABLE_LAPACK=OFF \
-DKokkos_ENABLE_SERIAL=ON \
Expand Down
4 changes: 2 additions & 2 deletions doc/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ lxml_html_clean

# Pre-install development lightning wheels
--extra-index-url https://test.pypi.org/simple/
pennylane-lightning==0.36.0
pennylane==0.36.0
pennylane-lightning-kokkos==0.37.0-dev42
pennylane-lightning==0.37.0-dev42
71 changes: 19 additions & 52 deletions frontend/catalyst/device/qjit_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def get_qjit_device_capabilities(target_capabilities: DeviceCapabilities) -> Set
qjit_capabilities.native_ops.update(
{
"MidCircuitMeasure": OperationProperties(
invertible=True, controllable=True, differentiable=False
invertible=False, controllable=False, differentiable=False
)
}
)
Expand Down Expand Up @@ -460,50 +460,21 @@ def execute(self, circuits, execution_config):
AnyQJITDevice = Union[QJITDevice, QJITDeviceNewAPI]


def filter_out_adjoint(operations):
"""Remove Adjoint from operations.
def filter_out_modifiers(operations):
"""Remove Adjoint/Control from operations.
Args:
operations (List[Str]): List of strings with names of supported operations
operations (Iterable[str]): list of operation names
Returns:
List: A list of strings with names of supported operations with Adjoint and C gates
removed.
Set: filtered set of operation names
"""
adjoint = re.compile(r"^Adjoint\(.*\)$")
c_adjoint = re.compile(r"^C\(Adjoint\(.*\)\)$")
adjoint_c = re.compile(r"^Adjoint\(C\(.*\)\)$")
pattern = re.compile(r"^(C|Adjoint)\(.*\)$")

def is_not_adj(op):
return not (re.match(adjoint, op) or re.match(c_adjoint, op) or re.match(adjoint_c, op))
def is_not_modifier(op):
return not re.match(pattern, op)

operations_no_adj = filter(is_not_adj, operations)
return set(operations_no_adj)


def check_no_overlap(*args, device_name):
"""Check items in *args are mutually exclusive.
Args:
*args (List[Str]): List of strings.
device_name (str): Device name for error reporting.
Raises:
CompileError
"""
set_of_sets = [set(arg) for arg in args]
union = set.union(*set_of_sets)
len_of_sets = [len(arg) for arg in args]
if sum(len_of_sets) == len(union):
return

overlaps = set()
for s in set_of_sets:
overlaps.update(s - union)
union = union - s

msg = f"Device '{device_name}' has overlapping gates: {overlaps}"
raise CompileError(msg)
return set(filter(is_not_modifier, operations))


@debug_logger
Expand Down Expand Up @@ -533,23 +504,19 @@ def validate_device_capabilities(

device_name = device.short_name if isinstance(device, qml.devices.LegacyDevice) else device.name

native = pennylane_operation_set(device_capabilities.native_ops)
decomposable = pennylane_operation_set(device_capabilities.to_decomp_ops)
matrix = pennylane_operation_set(device_capabilities.to_matrix_ops)
native = set(device_capabilities.native_ops.keys())
decomposable = set(device_capabilities.to_decomp_ops.keys())
matrix = set(device_capabilities.to_matrix_ops.keys())

check_no_overlap(native, decomposable, matrix, device_name=device_name)
overlap = (native & decomposable) | (native & matrix) | (decomposable & matrix)
if overlap:
raise CompileError(f"Device '{device_name}' has overlapping gates: {overlap}")

if hasattr(device, "operations") and hasattr(device, "observables"):
# For gates, we require strict match
# TODO: Eliminate string based matching against device declaration, e.g. lightning includes
# C(gate) (for some gates), but not Adjoint(gate), or C(Adjoint(gate)) ...
device_gates = filter_out_adjoint(set(device.operations))
# Lightning-kokkis might support C(GlobalPhase) in Python, but not in C++. We remove this
# gate before calling the validation.
# See https://github.com/PennyLaneAI/pennylane-lightning/pull/642#discussion_r1535478642
if device_name == "lightning.kokkos":
device_gates = device_gates - {"C(GlobalPhase)"}
spec_gates = filter_out_adjoint(set.union(native, matrix, decomposable))
# For validation against PL device properties we only check base gates as Adjoint/Control
# declarations can be very sporadic.
device_gates = filter_out_modifiers(device.operations)
spec_gates = native | decomposable | matrix
if device_gates != spec_gates:
raise CompileError(
"Gates in qml.device.operations and specification file do not match for "
Expand Down
9 changes: 1 addition & 8 deletions frontend/test/pytest/test_config_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import pytest

from catalyst.device import QJITDeviceNewAPI
from catalyst.device.qjit_device import check_no_overlap, validate_device_capabilities
from catalyst.device.qjit_device import validate_device_capabilities
from catalyst.utils.exceptions import CompileError
from catalyst.utils.toml import (
DeviceCapabilities,
Expand Down Expand Up @@ -302,13 +302,6 @@ def test_get_matrix_decomposable_gates_schema2():
assert "PauliZ" in device_capabilities.to_matrix_ops


def test_check_overlap_msg():
"""Test error is raised if there is an overlap in sets."""
msg = "Device 'test' has overlapping gates."
with pytest.raises(CompileError, match=msg):
check_no_overlap(["A"], ["A"], ["A"], device_name="test")


def test_config_invalid_attr():
"""Check the gate condition handling logic"""
with pytest.raises(
Expand Down
2 changes: 1 addition & 1 deletion frontend/test/pytest/test_gradient.py
Original file line number Diff line number Diff line change
Expand Up @@ -1151,7 +1151,7 @@ def f(x, y):
assert np.allclose(flatten_res_jax, flatten_res_catalyst)


@pytest.mark.xfail(reason="QubitUnitrary is not support with catalyst.grad")
@pytest.mark.xfail(reason="The verifier currently doesn't distinguish between active/inactive ops")
@pytest.mark.parametrize("inp", [(1.0), (2.0), (3.0), (4.0)])
def test_adj_qubitunitary(inp, backend):
"""Test the adjoint method."""
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ pytest-mock
nbmake

# optional rt/test dependencies
pennylane-lightning[kokkos]
pennylane-lightning-kokkos
amazon-braket-pennylane-plugin>1.27.1
2 changes: 1 addition & 1 deletion runtime/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ ENABLE_LIGHTNING?=ON
ENABLE_LIGHTNING_KOKKOS?=ON
ENABLE_OPENQASM?=ON
ENABLE_ASAN?=OFF
LIGHTNING_GIT_TAG_VALUE?=latest_release
LIGHTNING_GIT_TAG_VALUE?=476cac2e39f79c122b8751a77aad8139b39296db
ENABLE_LAPACK?=OFF

BUILD_TARGETS := rt_capi rtd_dummy
Expand Down
110 changes: 0 additions & 110 deletions runtime/lib/backend/lightning/lightning.kokkos.schema2.toml

This file was deleted.

Loading

0 comments on commit 5612227

Please sign in to comment.