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

[Frontend] Make tests toml-schema independent #712

Merged
merged 27 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
0ede63d
Make a test tomle-schema-2-compatible
May 3, 2024
1babc99
Address formatting issues
May 3, 2024
30de70c
Make test toml-schema-independant
May 6, 2024
9f985b0
Merge branch 'main' into toml-schema-2-update-test
May 6, 2024
f97e7e9
Address codecov errors
May 6, 2024
40499b4
Address codecov errors
May 6, 2024
0ed92b6
Address codecov errors
May 6, 2024
22cfe45
Merge remote-tracking branch 'origin/main' into toml-schema-2-update-…
May 9, 2024
d00c7e4
Fix wrong field name
May 9, 2024
d3ffae5
Address review suggestions; Move code around
May 16, 2024
aa37716
Merge remote-tracking branch 'origin/main' into toml-schema-2-update-…
May 16, 2024
6537b2a
Address formatting issues
May 16, 2024
468c3ca
Address formatting issues
May 16, 2024
72c9f24
Address pylint issues
May 16, 2024
611e5fc
Add missing paths module
May 16, 2024
e94c55d
Merge branch 'main' into toml-schema-2-update-test
May 21, 2024
5bd08cb
Fix re-added runtime.py
May 22, 2024
f59e6bc
Fix a test
May 22, 2024
8aadcf4
Merge remote-tracking branch 'origin/main' into toml-schema-2-update-…
May 22, 2024
1ba9149
Update frontend/catalyst/jax_primitives.py
May 24, 2024
4735b85
Update frontend/catalyst/third_party/cuda/primitives/__init__.py
May 24, 2024
74ec4ff
Rename paths -> runtime_environment
May 24, 2024
8793214
Address review suggestions: move validate_device_requirements -> qjit…
May 24, 2024
04a5b41
Address review suggestions: remove self.qjit_device attribute from qn…
May 24, 2024
2bb3ede
Fix a test import
May 24, 2024
722886c
Merge branch 'main' into toml-schema-2-update-test
May 24, 2024
eadeb91
Update changelog
May 24, 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
12 changes: 8 additions & 4 deletions doc/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,14 @@
annotations.
[(#751)](https://github.com/PennyLaneAI/catalyst/pull/751)

* Refactored `vmap` decorator in order to follow a unified pattern that uses a callable
class that implements the decorator's logic. This prevents having to excessively define
* Refactored `vmap` decorator in order to follow a unified pattern that uses a callable
class that implements the decorator's logic. This prevents having to excessively define
functions in a nested fashion.
[(#758)](https://github.com/PennyLaneAI/catalyst/pull/758)

* Catalyst tests now manipulate device capabilities rather than text configurations files.
[(#712)](https://github.com/PennyLaneAI/catalyst/pull/712)
Comment on lines +100 to +101
Copy link
Collaborator

@dime10 dime10 May 24, 2024

Choose a reason for hiding this comment

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

Reading this entry it sounds like it only concerns tests. Typically we exclude changelog entries for test-only, ci-only, doc-only, and repo-only changes.
Maybe the refactor can be mentioned in internal changes though? 🤔 New features, improvements, bug fixes should all be observable by the user in the distributed release in some way.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok done

Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think the commit changed anything?


<h3>Breaking changes</h3>

* Binary distributions for Linux are now based on `manylinux_2_28` instead of `manylinux_2014`.
Expand Down Expand Up @@ -198,11 +201,12 @@
This release contains contributions from (in alphabetical order):

David Ittah,
Mehrdad Malekmohammadi,
Erick Ochoa,
Haochen Paul Wang,
Lee James O'Riordan,
Mehrdad Malekmohammadi,
Raul Torres,
Haochen Paul Wang.
Sergei Mironov.

# Release 0.6.0

Expand Down
2 changes: 1 addition & 1 deletion frontend/catalyst/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

from catalyst.utils.exceptions import CompileError
from catalyst.utils.filesystem import Directory
from catalyst.utils.runtime import get_lib_path
from catalyst.utils.runtime_environment import get_lib_path

package_root = os.path.dirname(__file__)

Expand Down
11 changes: 10 additions & 1 deletion frontend/catalyst/device/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,18 @@
Internal API for the device module.
"""

from catalyst.device.qjit_device import QJITDevice, QJITDeviceNewAPI
from catalyst.device.qjit_device import (
BackendInfo,
QJITDevice,
QJITDeviceNewAPI,
extract_backend_info,
validate_device_capabilities,
)

__all__ = (
"QJITDevice",
"QJITDeviceNewAPI",
"BackendInfo",
"extract_backend_info",
"validate_device_capabilities",
)
220 changes: 195 additions & 25 deletions frontend/catalyst/device/qjit_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@
This module contains device stubs for the old and new PennyLane device API, which facilitate
the application of decomposition and other device pre-processing routines.
"""

import os
import pathlib
import platform
import re
from copy import deepcopy
from dataclasses import dataclass
from functools import partial
from typing import Optional, Set
from typing import Any, Dict, Optional, Set

import pennylane as qml
from pennylane.measurements import MidMeasureMP
Expand All @@ -32,13 +36,10 @@
)
from catalyst.utils.exceptions import CompileError
from catalyst.utils.patching import Patcher
from catalyst.utils.runtime import BackendInfo, device_get_toml_config
from catalyst.utils.runtime_environment import get_lib_path
from catalyst.utils.toml import (
DeviceCapabilities,
OperationProperties,
ProgramFeatures,
TOMLDocument,
get_device_capabilities,
intersect_operations,
pennylane_operation_set,
)
Expand Down Expand Up @@ -84,6 +85,87 @@
for op in RUNTIME_OPERATIONS
}

# TODO: This should be removed after implementing `get_c_interface`
# for the following backend devices:
SUPPORTED_RT_DEVICES = {
"lightning.qubit": ("LightningSimulator", "librtd_lightning"),
"lightning.kokkos": ("LightningKokkosSimulator", "librtd_lightning"),
"braket.aws.qubit": ("OpenQasmDevice", "librtd_openqasm"),
"braket.local.qubit": ("OpenQasmDevice", "librtd_openqasm"),
}


@dataclass
class BackendInfo:
"""Backend information"""

device_name: str
c_interface_name: str
lpath: str
kwargs: Dict[str, Any]


def extract_backend_info(device: qml.QubitDevice, capabilities: DeviceCapabilities) -> BackendInfo:
"""Extract the backend info from a quantum device. The device is expected to carry a reference
to a valid TOML config file."""
# pylint: disable=too-many-branches

dname = device.name
if isinstance(device, qml.Device):
dname = device.short_name

device_name = ""
device_lpath = ""
device_kwargs = {}

if dname in SUPPORTED_RT_DEVICES:
# Support backend devices without `get_c_interface`
device_name = SUPPORTED_RT_DEVICES[dname][0]
device_lpath = get_lib_path("runtime", "RUNTIME_LIB_DIR")
sys_platform = platform.system()

if sys_platform == "Linux":
device_lpath = os.path.join(device_lpath, SUPPORTED_RT_DEVICES[dname][1] + ".so")
elif sys_platform == "Darwin": # pragma: no cover
device_lpath = os.path.join(device_lpath, SUPPORTED_RT_DEVICES[dname][1] + ".dylib")
else: # pragma: no cover
raise NotImplementedError(f"Platform not supported: {sys_platform}")
elif hasattr(device, "get_c_interface"):
# Support third party devices with `get_c_interface`
device_name, device_lpath = device.get_c_interface()
else:
raise CompileError(f"The {dname} device does not provide C interface for compilation.")

if not pathlib.Path(device_lpath).is_file():
raise CompileError(f"Device at {device_lpath} cannot be found!")

if hasattr(device, "shots"):
if isinstance(device, qml.Device):
device_kwargs["shots"] = device.shots if device.shots else 0
else:
# TODO: support shot vectors
device_kwargs["shots"] = device.shots.total_shots if device.shots else 0

if dname == "braket.local.qubit": # pragma: no cover
device_kwargs["device_type"] = dname
device_kwargs["backend"] = (
# pylint: disable=protected-access
device._device._delegate.DEVICE_ID
)
elif dname == "braket.aws.qubit": # pragma: no cover
device_kwargs["device_type"] = dname
device_kwargs["device_arn"] = device._device._arn # pylint: disable=protected-access
if device._s3_folder: # pylint: disable=protected-access
device_kwargs["s3_destination_folder"] = str(
device._s3_folder # pylint: disable=protected-access
)

for k, v in capabilities.options.items():
if hasattr(device, v):
device_kwargs[k] = getattr(device, v)

Check notice on line 166 in frontend/catalyst/device/qjit_device.py

View check run for this annotation

codefactor.io / CodeFactor

frontend/catalyst/device/qjit_device.py#L108-L166

Complex Method
return BackendInfo(dname, device_name, device_lpath, device_kwargs)

sergei-mironov marked this conversation as resolved.
Show resolved Hide resolved

def get_qjit_device_capabilities(target_capabilities: DeviceCapabilities) -> Set[str]:
"""Calculate the set of supported quantum gates for the QJIT device from the gates
Expand Down Expand Up @@ -165,7 +247,7 @@

def __init__(
self,
target_config: TOMLDocument,
original_device_capabilities: DeviceCapabilities,
shots=None,
wires=None,
backend: Optional[BackendInfo] = None,
Expand All @@ -175,23 +257,18 @@
self.backend_name = backend.c_interface_name if backend else "default"
self.backend_lib = backend.lpath if backend else ""
self.backend_kwargs = backend.kwargs if backend else {}
device_name = backend.device_name if backend else "default"

program_features = ProgramFeatures(shots is not None)
target_device_capabilities = get_device_capabilities(
target_config, program_features, device_name
)
self.capabilities = get_qjit_device_capabilities(target_device_capabilities)
self.qjit_capabilities = get_qjit_device_capabilities(original_device_capabilities)

@property
def operations(self) -> Set[str]:
"""Get the device operations using PennyLane's syntax"""
return pennylane_operation_set(self.capabilities.native_ops)
return pennylane_operation_set(self.qjit_capabilities.native_ops)

@property
def observables(self) -> Set[str]:
"""Get the device observables"""
return pennylane_operation_set(self.capabilities.native_obs)
return pennylane_operation_set(self.qjit_capabilities.native_obs)

def apply(self, operations, **kwargs):
"""
Expand Down Expand Up @@ -270,6 +347,7 @@
def __init__(
self,
original_device,
original_device_capabilities: DeviceCapabilities,
backend: Optional[BackendInfo] = None,
):
self.original_device = original_device
Expand All @@ -285,29 +363,23 @@
self.backend_name = backend.c_interface_name if backend else "default"
self.backend_lib = backend.lpath if backend else ""
self.backend_kwargs = backend.kwargs if backend else {}
device_name = backend.device_name if backend else "default"

target_config = device_get_toml_config(original_device)
program_features = ProgramFeatures(original_device.shots is not None)
target_device_capabilities = get_device_capabilities(
target_config, program_features, device_name
)
self.capabilities = get_qjit_device_capabilities(target_device_capabilities)
self.qjit_capabilities = get_qjit_device_capabilities(original_device_capabilities)

@property
def operations(self) -> Set[str]:
"""Get the device operations"""
return pennylane_operation_set(self.capabilities.native_ops)
return pennylane_operation_set(self.qjit_capabilities.native_ops)

@property
def observables(self) -> Set[str]:
"""Get the device observables"""
return pennylane_operation_set(self.capabilities.native_obs)
return pennylane_operation_set(self.qjit_capabilities.native_obs)

@property
def measurement_processes(self) -> Set[str]:
"""Get the device measurement processes"""
return self.capabilities.measurement_processes
return self.qjit_capabilities.measurement_processes

def preprocess(
self,
Expand All @@ -334,3 +406,101 @@
Raises: RuntimeError
"""
raise RuntimeError("QJIT devices cannot execute tapes.")


def filter_out_adjoint(operations):
"""Remove Adjoint from operations.

Args:
operations (List[Str]): List of strings with names of supported operations

Returns:
List: A list of strings with names of supported operations with Adjoint and C gates
removed.
"""
adjoint = re.compile(r"^Adjoint\(.*\)$")

def is_not_adj(op):
return not re.match(adjoint, 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)


def validate_device_capabilities(
device: qml.QubitDevice, device_capabilities: DeviceCapabilities
) -> None:
"""Validate configuration document against the device attributes.
Raise CompileError in case of mismatch:
* If device is not qjit-compatible.
* If configuration file does not exists.
* If decomposable, matrix, and native gates have some overlap.
* If decomposable, matrix, and native gates do not match gates in ``device.operations`` and
``device.observables``.

Args:
device (qml.Device): An instance of a quantum device.
config (TOMLDocument): A TOML document representation.

Raises: CompileError
"""

if not device_capabilities.qjit_compatible_flag:
raise CompileError(
f"Attempting to compile program for incompatible device '{device.name}': "
f"Config is not marked as qjit-compatible"
)

device_name = device.short_name if isinstance(device, qml.Device) 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)

check_no_overlap(native, decomposable, matrix, device_name=device_name)

if hasattr(device, "operations") and hasattr(device, "observables"):
# For gates, we require strict match
device_gates = filter_out_adjoint(set(device.operations))
spec_gates = filter_out_adjoint(set.union(native, matrix, decomposable))
if device_gates != spec_gates:
raise CompileError(

Check warning on line 492 in frontend/catalyst/device/qjit_device.py

View check run for this annotation

Codecov / codecov/patch

frontend/catalyst/device/qjit_device.py#L492

Added line #L492 was not covered by tests
"Gates in qml.device.operations and specification file do not match.\n"
f"Gates that present only in the device: {device_gates - spec_gates}\n"
f"Gates that present only in spec: {spec_gates - device_gates}\n"
)

# For observables, we do not have `non-native` section in the config, so we check that
# device data supercedes the specification.
device_observables = set(device.observables)
spec_observables = pennylane_operation_set(device_capabilities.native_obs)
if (spec_observables - device_observables) != set():
raise CompileError(

Check warning on line 503 in frontend/catalyst/device/qjit_device.py

View check run for this annotation

Codecov / codecov/patch

frontend/catalyst/device/qjit_device.py#L503

Added line #L503 was not covered by tests
"Observables in qml.device.observables and specification file do not match.\n"
f"Observables that present only in spec: {spec_observables - device_observables}\n"
)
Loading
Loading