Skip to content

Commit

Permalink
Merge branch 'main' into tnguyen/noise-model-v2
Browse files Browse the repository at this point in the history
  • Loading branch information
1tnguyen authored Sep 26, 2024
2 parents 2764954 + f5ab479 commit 6b25ed5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 31 deletions.
67 changes: 39 additions & 28 deletions python/cudaq/handlers/photonics_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@

from ..mlir._mlir_libs._quakeDialects import cudaq_runtime

_TARGET_NAME = 'photonics'

# The qudit level must be explicitly defined
globalQuditLevel = None


@dataclass
class PyQudit:
Expand All @@ -34,14 +29,39 @@ class PyQudit:
level: int
id: int

def __del__(self):
try:
cudaq_runtime.photonics.release_qudit(self.level, self.id)
except Exception as e:
if _TARGET_NAME == cudaq_runtime.get_target().name:
raise e
else:
pass

class QuditManager(object):
"""
A class to explicitly manage resource allocation for qudits within a
`PhotonicsKernel`.
"""
qudit_level = None
allocated_ids = []

@classmethod
def reset(cls):
cls.qudit_level = None
cls.allocated_ids = []

@classmethod
def allocate(cls, level: int):
if cls.qudit_level is None:
cls.qudit_level = level
elif level != cls.qudit_level:
raise RuntimeError(
"The qudits must be of same level within a kernel.")
id = cudaq_runtime.photonics.allocate_qudit(cls.qudit_level)
cls.allocated_ids.append(id)
return PyQudit(cls.qudit_level, id)

def __enter__(cls):
cls.reset()

def __exit__(cls, exc_type, exc_val, exc_tb):
while cls.allocated_ids:
cudaq_runtime.photonics.release_qudit(cls.allocated_ids.pop(),
cls.qudit_level)
cls.reset()


def _is_qudit_type(q: any) -> bool:
Expand Down Expand Up @@ -71,7 +91,7 @@ def _check_args(q: any):
RuntimeError: If the qudit level is not set.
Exception: If input argument is not instance of `PyQudit` class.
"""
if globalQuditLevel is None:
if QuditManager.qudit_level is None:
raise RuntimeError(
"Qudit level not set. Define a qudit (`qudit(level=N)`) or list of qudits."
)
Expand All @@ -97,15 +117,7 @@ def qudit(level: int) -> PyQudit:
RuntimeError: If a qudit of level different than one already defined
in the kernel is requested.
"""
global globalQuditLevel

if globalQuditLevel is None:
globalQuditLevel = level
elif level != globalQuditLevel:
raise RuntimeError("The qudits must be of same level within a kernel.")

id = cudaq_runtime.photonics.allocate_qudit(globalQuditLevel)
return PyQudit(globalQuditLevel, id)
return QuditManager.allocate(level)


def plus(qudit: PyQudit):
Expand Down Expand Up @@ -202,13 +214,11 @@ class PhotonicsHandler(object):

def __init__(self, function):

if _TARGET_NAME != cudaq_runtime.get_target().name:
if 'photonics' != cudaq_runtime.get_target().name:
raise RuntimeError(
"A photonics kernel can only be used with 'photonics' target.")

global globalQuditLevel
globalQuditLevel = None

QuditManager.reset()
self.kernelFunction = function

self.kernelFunction.__globals__['qudit'] = qudit
Expand All @@ -218,4 +228,5 @@ def __init__(self, function):
self.kernelFunction.__globals__['mz'] = mz

def __call__(self, *args):
return self.kernelFunction(*args)
with QuditManager():
return self.kernelFunction(*args)
3 changes: 0 additions & 3 deletions python/tests/handlers/test_photonics_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import pytest

import gc
from typing import List

import cudaq
Expand All @@ -20,8 +19,6 @@ def do_something():
yield
cudaq.reset_target()
cudaq.__clearKernelRegistries()
# Make the tests stable by enforcing resource release
gc.collect()


def test_qudit():
Expand Down

0 comments on commit 6b25ed5

Please sign in to comment.