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

Remove CPhase gate alias references #718

Merged
merged 20 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 7 additions & 1 deletion frontend/catalyst/device/qjit_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,66 +105,66 @@
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 167 in frontend/catalyst/device/qjit_device.py

View check run for this annotation

codefactor.io / CodeFactor

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

Complex Method


def get_qjit_device_capabilities(target_capabilities: DeviceCapabilities) -> Set[str]:
Expand Down Expand Up @@ -487,10 +487,16 @@
if hasattr(device, "operations") and hasattr(device, "observables"):
# For gates, we require strict match
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))
if device_gates != spec_gates:
raise CompileError(
"Gates in qml.device.operations and specification file do not match.\n"
"Gates in qml.device.operations and specification file do not match for "
f'"{device_name}".\n'

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

View check run for this annotation

Codecov / codecov/patch

frontend/catalyst/device/qjit_device.py#L499

Added line #L499 was not covered by tests
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"
)
Expand Down
17 changes: 1 addition & 16 deletions frontend/catalyst/utils/toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,24 +260,10 @@ def get_operation_properties(config_props: dict) -> OperationProperties:


def patch_schema1_collections(
config, device_name, native_gate_props, matrix_decomp_props, decomp_props, observable_props
config, _device_name, native_gate_props, matrix_decomp_props, decomp_props
): # pylint: disable=too-many-arguments, too-many-branches
"""For old schema1 config files we deduce some information which was not explicitly encoded."""

# TODO: remove after PR #642 is merged in lightning
# NOTE: we mark GlobalPhase as controllables even if `quantum_control` flag is False. This
# is what actual device reports.
if device_name == "lightning.kokkos": # pragma: nocover
native_gate_props["GlobalPhase"] = OperationProperties(
invertible=False, controllable=True, differentiable=True
)

# TODO: remove after PR #642 is merged in lightning
if device_name == "lightning.kokkos": # pragma: nocover
observable_props["Projector"] = OperationProperties(
invertible=False, controllable=False, differentiable=False
)

# The deduction logic is the following:
# * Most of the gates have their `C(Gate)` controlled counterparts.
# * Some gates have to be decomposed if controlled version is used. Typically these are
Expand Down Expand Up @@ -414,7 +400,6 @@ def load_device_capabilities(
native_gate_props,
matrix_decomp_props,
decomp_props,
observable_props,
)

return DeviceCapabilities(
Expand Down
1 change: 0 additions & 1 deletion frontend/test/pytest/test_custom_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
"PSWAP",
"SISWAP",
"SQISW",
"CPhase",
"BasisState",
"QubitStateVector",
"StatePrep",
Expand Down
1 change: 0 additions & 1 deletion runtime/lib/backend/dummy/dummy_device.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ CY = { properties = [ "invertible", "differentiable" ] }
CZ = { properties = [ "invertible", "differentiable" ] }
PhaseShift = { properties = [ "controllable", "invertible", "differentiable" ] }
ControlledPhaseShift = { properties = [ "invertible", "differentiable" ] }
CPhase = { properties = [ "invertible", "differentiable" ] }
RX = { properties = [ "controllable", "invertible", "differentiable" ] }
RY = { properties = [ "controllable", "invertible", "differentiable" ] }
RZ = { properties = [ "controllable", "invertible", "differentiable" ] }
Expand Down
Loading