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

[python] qvector init from lists and arrays #1688

Merged
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
ebd7574
Add support for initialization of qvector from lists
annagrin Apr 25, 2024
aa542b1
Fix formatting changes
annagrin May 7, 2024
138174f
Merge branch 'experimental/stateHandling' of https://github.com/NVIDI…
annagrin May 7, 2024
495ce33
Support nd.array creation with dtypes and creating qvector from it
annagrin May 9, 2024
a65fd11
Merge branch 'experimental/stateHandling' of https://github.com/NVIDI…
annagrin May 10, 2024
b96ea19
temp
annagrin May 13, 2024
45bab2a
Add automatic conversion to the simulation precision data type
annagrin May 14, 2024
8964dfa
Use simluation dtype for alloca operation, add length checking for sc…
annagrin May 14, 2024
06b405f
Merge branch 'experimental/stateHandling' of https://github.com/NVIDI…
annagrin May 14, 2024
4abd9c2
Support creating nd arrays and initializing vectors. Add a vector cop…
annagrin May 15, 2024
5bf877f
Merge branch 'experimental/stateHandling' of https://github.com/NVIDI…
annagrin May 15, 2024
38eb835
Merge branch 'experimental/stateHandling' of https://github.com/NVIDI…
annagrin May 15, 2024
742e7ff
Fixed spelling
annagrin May 15, 2024
f84a810
Remove dictionary.dic
annagrin May 15, 2024
d938e29
Support cudaq.amplitudes inside kernels
annagrin May 16, 2024
4444e2d
Fixed test failures
annagrin May 16, 2024
d95794f
Merge branch 'experimental/stateHandling' of https://github.com/NVIDI…
annagrin May 16, 2024
6f06de7
Updated test
annagrin May 16, 2024
b7a6eaa
Fixed more failing tests
annagrin May 16, 2024
03c9f8b
Remove creating qvector of const length to be handled later
annagrin May 16, 2024
ac76461
Make vectors in test normalized
annagrin May 17, 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 python/cudaq/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def complex():
target = get_target()
precision = target.get_precision()
if precision == cudaq_runtime.SimulationPrecision.fp64:
return complex
return numpy.complex128
return numpy.complex64


Expand Down
5 changes: 3 additions & 2 deletions python/cudaq/kernel/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,9 @@ def visit_FunctionDef(self, node):
raise RuntimeError(
'cudaq.kernel functions must have argument type annotations.'
)
if isinstance(annotation,
ast.Subscript) and annotation.value.id == 'Callable':
if isinstance(annotation, ast.Subscript) and hasattr(
annotation.value,
"id") and annotation.value.id == 'Callable':
if not hasattr(annotation, 'slice'):
raise RuntimeError(
'Callable type must have signature specified.')
Expand Down
282 changes: 257 additions & 25 deletions python/cudaq/kernel/ast_bridge.py

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions python/cudaq/kernel/kernel_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ def __processArgType(self, ty):
"""
if ty in [cudaq_runtime.qvector, cudaq_runtime.qubit]:
return ty, None
if get_origin(ty) == list or isinstance(ty(), list):
if get_origin(ty) == list or isinstance(ty, list):
if '[' in str(ty) and ']' in str(ty):
allowedTypeMap = {
'int': int,
Expand Down Expand Up @@ -685,8 +685,10 @@ def qalloc(self, initializer=None):
self.ctx,
cc.StdvecType.getElementType(
initializer.mlirValue.type))
initials = cc.StdvecDataOp(ptrTy, initializer.mlirValue)
quake.InitializeStateOp(veqTy, qubits, initials)
initials = cc.StdvecDataOp(ptrTy,
initializer.mlirValue).result
qubits = quake.InitializeStateOp(veqTy, qubits,
initials).result
return self.__createQuakeValue(qubits)

# If no initializer, create a single qubit
Expand Down
4 changes: 2 additions & 2 deletions python/cudaq/kernel/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@


class Color:
YELLOW = '\033[93m'
RED = '\033[91m'
BOLD = '\033[1m'
END = '\033[0m'
Expand Down Expand Up @@ -92,7 +93,7 @@ def emitFatalErrorOverride(msg):
return cc.CharspanType.get(ctx)

if annotation.value.id in ['numpy', 'np']:
if annotation.attr == 'ndarray':
if annotation.attr in ['array', 'ndarray']:
return cc.StdvecType.get(ctx, F64Type.get())
if annotation.attr == 'complex128':
return ComplexType.get(F64Type.get())
Expand Down Expand Up @@ -176,7 +177,6 @@ def emitFatalErrorOverride(msg):


def mlirTypeFromPyType(argType, ctx, **kwargs):

if argType == int:
return IntegerType.get_signless(64, ctx)
if argType in [float, np.float64]:
Expand Down
52 changes: 43 additions & 9 deletions python/tests/builder/test_kernel_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -877,10 +877,11 @@ def test_recursive_calls():

print(kernel3)


skipIfNvidiaFP64NotInstalled = pytest.mark.skipif(
not (cudaq.num_available_gpus() > 0 and cudaq.has_target('nvidia-fp64')),
reason='Could not find nvidia-fp64 in installation')
not (cudaq.num_available_gpus() > 0 and cudaq.has_target('nvidia-fp64')),
reason='Could not find nvidia-fp64 in installation')


@skipIfNvidiaFP64NotInstalled
def test_from_state0():
Expand Down Expand Up @@ -944,10 +945,12 @@ def test_from_state0():

cudaq.reset_target()


skipIfNvidiaNotInstalled = pytest.mark.skipif(
not (cudaq.num_available_gpus() > 0 and cudaq.has_target('nvidia')),
reason='Could not find nvidia in installation')

not (cudaq.num_available_gpus() > 0 and cudaq.has_target('nvidia')),
reason='Could not find nvidia in installation')


@skipIfNvidiaNotInstalled
def test_from_state1():
cudaq.set_target('nvidia')
Expand All @@ -969,8 +972,7 @@ def test_from_state1():

# Regardless of the target precision, use
# cudaq.complex() or cudaq.amplitudes()
state = np.array([.70710678, 0., 0., 0.70710678],
dtype=cudaq.complex())
state = np.array([.70710678, 0., 0., 0.70710678], dtype=cudaq.complex())
kernel2 = cudaq.make_kernel()
qubits = kernel2.qalloc(state)
counts = cudaq.sample(kernel2)
Expand All @@ -986,7 +988,7 @@ def test_from_state1():
assert '11' in counts
assert '00' in counts

state = cudaq.amplitudes(np.array([.5]*4))
state = cudaq.amplitudes(np.array([.5] * 4))
kernel2 = cudaq.make_kernel()
qubits = kernel2.qalloc(state)
counts = cudaq.sample(kernel2)
Expand Down Expand Up @@ -1358,6 +1360,38 @@ def test_u3_ctrl():
assert ('11' in counts)


@skipIfNvidiaFP64NotInstalled
def test_builder_rotate_state():
cudaq.reset_target()
cudaq.set_target('nvidia-fp64')

c = [0., 0., 0., 1.]

# Our kernel will start with 2 qubits in `11`, then
# rotate each qubit back to `0` before applying a
# Hadamard gate.
kernel, state = cudaq.make_kernel(list[complex])
q = kernel.qalloc(state)

# Can now operate on the qvector as usual:
# Rotate state of the front qubit 180 degrees along X.
kernel.x(q[0])
# Rotate state of the back qubit 180 degrees along Y.
kernel.y(q[1])
# Put qubits into superposition state.
kernel.h(q)

# Measure.
kernel.mz(q)

counts = cudaq.sample(kernel, c)
print(counts)
assert '11' in counts
assert '00' in counts
assert '01' in counts
assert '10' in counts


# leave for gdb debugging
if __name__ == "__main__":
loc = os.path.abspath(__file__)
Expand Down
Loading
Loading