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

Solves Issue #1316: Implement an option to print out instructions in the null device #1346

Open
wants to merge 31 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
d015a7a
feat: adds print_instructions flag to qjit, and forwards to device co…
smml1996 Nov 23, 2024
f4170f2
refact: change comment
smml1996 Nov 23, 2024
88ceb13
fix: initialize earlier print_instructions flag in QJIT class
smml1996 Nov 28, 2024
c63f33c
feat: prints instruction in null device
smml1996 Nov 28, 2024
f966446
refact: makes some funcs private
smml1996 Nov 29, 2024
27da58d
fix: output hamiltonian does not has consecutive + -
smml1996 Nov 29, 2024
7add3c3
refact: make format
smml1996 Nov 29, 2024
27dd5cf
fix: printing bugs
smml1996 Nov 29, 2024
a53e314
feat: tests
smml1996 Nov 29, 2024
84be479
refact: make format
smml1996 Nov 29, 2024
32cbf92
refact: removes commented test
smml1996 Nov 29, 2024
75d33c0
adds missing conventional final line to Test_InstructionStrBuilder.cpp
smml1996 Dec 1, 2024
32bcb4e
fix: codefactor-io too long lines
smml1996 Dec 1, 2024
d998f46
fix: namedOperation prints always wires=[..]
smml1996 Dec 1, 2024
afea6e3
include a non-trivial complex number for better coverage in MatrixOpe…
smml1996 Dec 1, 2024
f04edc7
fix: spacing of negative coefficients
smml1996 Dec 1, 2024
efdc884
refact: State() does not prints view
smml1996 Dec 1, 2024
f081130
fix: only create observables string when print_instructions=true
smml1996 Dec 1, 2024
cb35bfe
fixes comment typos
smml1996 Dec 1, 2024
18653b5
refact: changes names of funcs
smml1996 Dec 1, 2024
e4c3c89
Merge remote-tracking branch 'refs/remotes/origin/main'
smml1996 Dec 1, 2024
dccb962
make format
smml1996 Dec 1, 2024
b3df667
update changelog
smml1996 Dec 1, 2024
4c82c8d
Update changelog-dev.md
smml1996 Dec 1, 2024
eb6ad9d
refact: minor change
smml1996 Dec 1, 2024
a46779d
Merge remote-tracking branch 'refs/remotes/origin/main'
smml1996 Dec 1, 2024
8b5af55
removes string representation of DataViews
smml1996 Dec 2, 2024
975cb7d
removes blank line
smml1996 Dec 2, 2024
5ea6b8e
handle zero coefficient for real part in imaginary number string repr…
smml1996 Dec 2, 2024
e901dc2
Merge branch 'main' into main
smml1996 Dec 2, 2024
a5f7c30
fix: only pass print_instructions when using null.qubit device
smml1996 Dec 2, 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
6 changes: 5 additions & 1 deletion doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

<h3>New features since last release</h3>

* The PennyLane plugin now has the option to print out instructions in the null device
[(#1316)](https://github.com/PennyLaneAI/catalyst/pull/1346)

<h3>Improvements 🛠</h3>

* Replace pybind11 with nanobind for C++/Python bindings in the frontend and in the runtime.
Expand Down Expand Up @@ -113,4 +116,5 @@ Mehrdad Malekmohammadi,
William Maxwell
Romain Moyard,
Raul Torres,
Paul Haochen Wang.
Paul Haochen Wang,
Stefanie Muroya Lei.
7 changes: 6 additions & 1 deletion frontend/catalyst/device/qjit_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
# pylint: disable=too-many-branches
@debug_logger
def extract_backend_info(
device: qml.devices.QubitDevice, capabilities: DeviceCapabilities

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

View check run for this annotation

codefactor.io / CodeFactor

frontend/catalyst/device/qjit_device.py#L147

Unused argument 'capabilities' (unused-argument)
) -> BackendInfo:
"""Extract the backend info from a quantum device. The device is expected to carry a reference
to a valid TOML config file."""
Expand Down Expand Up @@ -303,7 +303,7 @@
return extract_backend_info(device, capabilities)

@debug_logger_init
def __init__(self, original_device):
def __init__(self, original_device, print_instructions=False):
self.original_device = original_device

for key, value in original_device.__dict__.items():
Expand Down Expand Up @@ -331,6 +331,11 @@
self.backend_name = backend.c_interface_name
self.backend_lib = backend.lpath
self.backend_kwargs = backend.kwargs

if original_device.name == "null.qubit":
# include 'print_instructions' as a keyword argument for the device constructor.
self.backend_kwargs["print_instructions"] = print_instructions

self.capabilities = get_qjit_device_capabilities(device_capabilities)

@debug_logger
Expand Down
17 changes: 15 additions & 2 deletions frontend/catalyst/jit.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def qjit(
seed=None,
experimental_capture=False,
circuit_transform_pipeline=None,
print_instructions=False
): # pylint: disable=too-many-arguments,unused-argument
"""A just-in-time decorator for PennyLane and JAX programs using Catalyst.

Expand Down Expand Up @@ -153,6 +154,8 @@ def qjit(
dictionaries of valid keyword arguments and values for the specific pass.
The order of keys in this dictionary will determine the pass pipeline.
If not specified, the default pass pipeline will be applied.
print_instructions (Optional[bool]):
If set to True, instructions are printed when executing in the null device.

Returns:
QJIT object.
Expand Down Expand Up @@ -430,11 +433,12 @@ def sum_abstracted(arr):
"""
kwargs = copy.copy(locals())
kwargs.pop("fn")
kwargs.pop("print_instructions")

if fn is None:
return functools.partial(qjit, **kwargs)

return QJIT(fn, CompileOptions(**kwargs))
return QJIT(fn, CompileOptions(**kwargs), print_instructions=print_instructions)


## IMPL ##
Expand All @@ -452,6 +456,8 @@ class QJIT(CatalystCallable):
Args:
fn (Callable): the quantum or classical function to compile
compile_options (CompileOptions): compilation options to use
print_instructions (Optional[bool]): If True, prints instructions
when executing in a null device.

:ivar original_function: This attribute stores `fn`, the quantum or classical function
object to compile, as is, without any modifications
Expand All @@ -462,7 +468,10 @@ class QJIT(CatalystCallable):
"""

@debug_logger_init
def __init__(self, fn, compile_options):
def __init__(self, fn, compile_options, print_instructions=False):
# flag for printing instructions in the null device
self.print_instructions = print_instructions

functools.update_wrapper(self, fn)
self.original_function = fn
self.compile_options = compile_options
Expand Down Expand Up @@ -635,6 +644,10 @@ def closure(qnode, *args, **kwargs):
params = {}
params["static_argnums"] = kwargs.pop("static_argnums", static_argnums)
params["_out_tree_expected"] = []

if qnode.device.name == "null.qubit":
kwargs["print_instructions"] = self.print_instructions

return QFunc.__call__(
qnode,
pass_pipeline=self.compile_options.circuit_transform_pipeline,
Expand Down
5 changes: 4 additions & 1 deletion frontend/catalyst/qfunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@ def __call__(self, *args, **kwargs):
mcm_config.postselect_mode = mcm_config.postselect_mode or "hw-like"
return Function(dynamic_one_shot(self, mcm_config=mcm_config))(*args, **kwargs)

qjit_device = QJITDevice(self.device)
# retrieve the flag to print instructions, used for executing
# pre-compiled programs in a null device.
print_instructions = kwargs.pop("print_instructions", False)
qjit_device = QJITDevice(self.device, print_instructions)

static_argnums = kwargs.pop("static_argnums", ())
out_tree_expected = kwargs.pop("_out_tree_expected", [])
Expand Down
Loading