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 15 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
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.paths import get_lib_path
sergei-mironov marked this conversation as resolved.
Show resolved Hide resolved

package_root = os.path.dirname(__file__)

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

from catalyst.device.qjit_device import QJITDevice, QJITDeviceNewAPI

__all__ = (
"QJITDevice",
"QJITDeviceNewAPI",
from catalyst.device.qjit_device import (
BackendInfo,
QJITDevice,
QJITDeviceNewAPI,
extract_backend_info,
)

__all__ = ("QJITDevice", "QJITDeviceNewAPI", "BackendInfo", "extract_backend_info")
121 changes: 96 additions & 25 deletions frontend/catalyst/device/qjit_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
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
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 +35,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.paths 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 +84,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)

return BackendInfo(dname, device_name, device_lpath, device_kwargs)

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

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 +246,7 @@

def __init__(
self,
target_config: TOMLDocument,
original_device_capabilities: DeviceCapabilities,
shots=None,
wires=None,
backend: Optional[BackendInfo] = None,
Expand All @@ -175,23 +256,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 +346,7 @@
def __init__(
self,
original_device,
original_device_capabilities: DeviceCapabilities,
backend: Optional[BackendInfo] = None,
):
self.original_device = original_device
Expand All @@ -285,29 +362,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 Down
2 changes: 1 addition & 1 deletion frontend/catalyst/jax_primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
from catalyst.utils.extra_bindings import FromElementsOp, TensorExtractOp
from catalyst.utils.types import convert_shaped_arrays_to_tensors

# pylint: disable=unused-argument,abstract-method,too-many-lines
# pylint: disable=unused-argument,too-many-lines
sergei-mironov marked this conversation as resolved.
Show resolved Hide resolved

#########
# Types #
Expand Down
44 changes: 28 additions & 16 deletions frontend/catalyst/qfunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,25 @@
from jax.core import eval_jaxpr
from jax.tree_util import tree_flatten, tree_unflatten

from catalyst.device import QJITDevice, QJITDeviceNewAPI
from catalyst.device import (
BackendInfo,
QJITDevice,
QJITDeviceNewAPI,
extract_backend_info,
)
from catalyst.jax_extras import (
deduce_avals,
get_implicit_and_explicit_flat_args,
unzip2,
)
from catalyst.jax_primitives import func_p
from catalyst.jax_tracer import trace_quantum_function
from catalyst.utils.runtime import (
BackendInfo,
device_get_toml_config,
extract_backend_info,
validate_config_with_device,
from catalyst.utils.toml import (
DeviceCapabilities,
ProgramFeatures,
get_device_capabilities,
validate_device_capabilities,
)
from catalyst.utils.toml import TOMLDocument


class QFunc:
Expand All @@ -54,26 +58,34 @@ def __new__(cls):
raise NotImplementedError() # pragma: no-cover

@staticmethod
def extract_backend_info(device: qml.QubitDevice, config: TOMLDocument) -> BackendInfo:
def extract_backend_info(
device: qml.QubitDevice, capabilities: DeviceCapabilities
) -> BackendInfo:
"""Wrapper around extract_backend_info in the runtime module."""
return extract_backend_info(device, config)
return extract_backend_info(device, capabilities)

# pylint: disable=no-member
# pylint: disable=no-member, attribute-defined-outside-init
def __call__(self, *args, **kwargs):
assert isinstance(self, qml.QNode)

config = device_get_toml_config(self.device)
validate_config_with_device(self.device, config)
backend_info = QFunc.extract_backend_info(self.device, config)
device = self.device
program_features = ProgramFeatures(device.shots is not None)
device_capabilities = get_device_capabilities(device, program_features)
backend_info = QFunc.extract_backend_info(device, device_capabilities)

# Validate decive operations against the declared capabilities
validate_device_capabilities(device, device_capabilities)
dime10 marked this conversation as resolved.
Show resolved Hide resolved

if isinstance(self.device, qml.devices.Device):
device = QJITDeviceNewAPI(self.device, backend_info)
self.qjit_device = QJITDeviceNewAPI(device, device_capabilities, backend_info)
else:
device = QJITDevice(config, self.device.shots, self.device.wires, backend_info)
self.qjit_device = QJITDevice(
device_capabilities, device.shots, device.wires, backend_info
)

def _eval_quantum(*args):
closed_jaxpr, out_type, out_tree = trace_quantum_function(
self.func, device, args, kwargs, qnode=self
self.func, self.qjit_device, args, kwargs, qnode=self
)
args_expanded = get_implicit_and_explicit_flat_args(None, *args)
res_expanded = eval_jaxpr(closed_jaxpr.jaxpr, closed_jaxpr.consts, *args_expanded)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import pennylane as qml
from jax.tree_util import tree_unflatten

from catalyst.device import BackendInfo
from catalyst.jax_primitives import (
AbstractObs,
adjoint_p,
Expand Down Expand Up @@ -75,7 +76,6 @@
from catalyst.qfunc import QFunc
from catalyst.utils.exceptions import CompileError
from catalyst.utils.patching import Patcher
from catalyst.utils.runtime import BackendInfo

from .primitives import (
cuda_inst,
Expand Down Expand Up @@ -820,7 +820,7 @@ def get_jaxpr(self, *args):
an MLIR module
"""

def cudaq_backend_info(device, _config) -> BackendInfo:
def cudaq_backend_info(device, _capabilities) -> BackendInfo:
"""The extract_backend_info should not be run by the cuda compiler as it is
catalyst-specific. We need to make this API a bit nicer for third-party compilers.
"""
Expand Down
2 changes: 1 addition & 1 deletion frontend/catalyst/third_party/cuda/primitives/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

# We disable protected access in particular to avoid warnings with cudaq._pycuda.
# And we disable unused-argument to avoid unused arguments in abstract_eval, particularly kwargs.
# pylint: disable=protected-access,unused-argument,abstract-method,line-too-long
# pylint: disable=protected-access,unused-argument,line-too-long
sergei-mironov marked this conversation as resolved.
Show resolved Hide resolved


class AbsCudaQState(jax.core.AbstractValue):
Expand Down
38 changes: 38 additions & 0 deletions frontend/catalyst/utils/paths.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright 2024 Xanadu Quantum Technologies Inc.

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Utility code for keeping paths
"""
import os
import os.path

from catalyst._configuration import INSTALLED

package_root = os.path.dirname(__file__)

# Default paths to dep libraries
DEFAULT_LIB_PATHS = {
"llvm": os.path.join(package_root, "../../../mlir/llvm-project/build/lib"),
"runtime": os.path.join(package_root, "../../../runtime/build/lib"),
"enzyme": os.path.join(package_root, "../../../mlir/Enzyme/build/Enzyme"),
"oqc_runtime": os.path.join(package_root, "../../catalyst/third_party/oqc/src/build"),
}


def get_lib_path(project, env_var):
"""Get the library path."""
if INSTALLED:
return os.path.join(package_root, "..", "lib") # pragma: no cover
return os.getenv(env_var, DEFAULT_LIB_PATHS.get(project, ""))
1 change: 1 addition & 0 deletions frontend/catalyst/utils/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ def validate_config_with_device(device: qml.QubitDevice, config: TOMLDocument) -

Raises: CompileError
"""
# pylint: disable=too-many-function-args

if not config["compilation"]["qjit_compatible"]:
raise CompileError(
Expand Down
Loading
Loading