-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix incorrect call return for python device kernels (#2319)
* Fix incorrect call result in python Signed-off-by: Anna Gringauze <[email protected]> * Temp Signed-off-by: Anna Gringauze <[email protected]> * Add more tests Signed-off-by: Anna Gringauze <[email protected]> * DCO Remediation Commit for Anna Gringauze <[email protected]> I, Anna Gringauze <[email protected]>, hereby add my Signed-off-by to this commit: ce0fd32 Signed-off-by: Anna Gringauze <[email protected]> * Cleanup Signed-off-by: Anna Gringauze <[email protected]> * Address CR comments Signed-off-by: Anna Gringauze <[email protected]> --------- Signed-off-by: Anna Gringauze <[email protected]>
- Loading branch information
Showing
2 changed files
with
116 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# ============================================================================ # | ||
# Copyright (c) 2022 - 2024 NVIDIA Corporation & Affiliates. # | ||
# All rights reserved. # | ||
# # | ||
# This source code and the accompanying materials are made available under # | ||
# the terms of the Apache License 2.0 which accompanies this distribution. # | ||
# ============================================================================ # | ||
|
||
import cudaq | ||
|
||
|
||
def test_call_with_callee_return_bool(): | ||
|
||
@cudaq.kernel | ||
def bar(qubits: cudaq.qview) -> bool: | ||
x(qubits) | ||
return False | ||
|
||
@cudaq.kernel | ||
def foo(n: int): | ||
qubits = cudaq.qvector(n) | ||
bar(qubits) | ||
|
||
counts = cudaq.sample(foo, 3) | ||
assert "111" in counts and len(counts) == 1 | ||
|
||
|
||
def test_call_with_return_bool(): | ||
|
||
@cudaq.kernel() | ||
def callee(q: cudaq.qubit) -> bool: | ||
x(q) | ||
m = mz(q) | ||
return m | ||
|
||
@cudaq.kernel() | ||
def caller() -> bool: | ||
q = cudaq.qubit() | ||
return callee(q) | ||
|
||
result = caller() | ||
assert result == True or result == False | ||
|
||
counts = cudaq.sample(caller) | ||
assert '1' in counts and len(counts) == 1 | ||
|
||
|
||
def test_call_with_return_bool2(): | ||
from dataclasses import dataclass | ||
|
||
@dataclass | ||
class patch: | ||
data: cudaq.qview | ||
ancx: cudaq.qview | ||
ancz: cudaq.qview | ||
|
||
@cudaq.kernel() | ||
def stabilizer(logicalQubit: patch, x_stabilizers: list[int], | ||
z_stabilizers: list[int]) -> bool: | ||
for xi in range(len(logicalQubit.ancx)): | ||
for di in range(len(logicalQubit.data)): | ||
if x_stabilizers[xi * len(logicalQubit.data) + di] == 1: | ||
x.ctrl(logicalQubit.ancx[xi], logicalQubit.data[di]) | ||
|
||
h(logicalQubit.ancx) | ||
for zi in range(len(logicalQubit.ancz)): | ||
for di in range(len(logicalQubit.data)): | ||
if z_stabilizers[zi * len(logicalQubit.data) + di] == 1: | ||
x.ctrl(logicalQubit.data[di], logicalQubit.ancz[zi]) | ||
|
||
results = mz(logicalQubit.ancx, logicalQubit.ancz) | ||
|
||
reset(logicalQubit.ancx) | ||
reset(logicalQubit.ancz) | ||
#TODO: support returning lists | ||
#Issue: https://github.com/NVIDIA/cuda-quantum/issues/2336 | ||
return results[3] | ||
|
||
@cudaq.kernel() | ||
def run() -> bool: | ||
q = cudaq.qvector(2) | ||
x(q[0]) | ||
r = cudaq.qvector(2) | ||
s = cudaq.qvector(2) | ||
p = patch(q, r, s) | ||
|
||
return stabilizer(p, [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1]) | ||
|
||
result = run() | ||
assert result == True or result == False | ||
|
||
sample_result = cudaq.sample(run) | ||
counts = sample_result.get_register_counts("results") | ||
assert len(counts) == 4 |