Skip to content

Commit 498a964

Browse files
cudaq export fix discrete gates; up version
1 parent d8444ef commit 498a964

7 files changed

Lines changed: 34 additions & 16 deletions

File tree

genQC/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.2.1"
1+
__version__ = "0.2.2"

genQC/inference/sampling.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,9 @@ def decode_tensors_to_backend(simulator: Simulator,
306306
params: Optional[torch.Tensor] = None,
307307
silent: bool = True,
308308
n_jobs: int = 1,
309-
filter_errs: bool = True) -> tuple[Sequence[any], int]:
309+
filter_errs: bool = True,
310+
return_tensors: bool = False,
311+
) -> tuple[Sequence[any], int] | tuple[Sequence[any], int, torch.Tensor]:
310312
tensors = tensors.cpu()
311313

312314
if exists(params):
@@ -333,8 +335,14 @@ def _f(iter_vars):
333335
backend_obj_list = [pot_qc for pot_qc in pot_qcs if exists(pot_qc)]
334336
err_cnt = sum(1 for pot_qc in pot_qcs if not_exists(pot_qc))
335337
assert len(backend_obj_list) + err_cnt == len(pot_qcs)
338+
339+
if return_tensors:
340+
tensors = tensors[torch.tensor([exists(pot_qc) for pot_qc in pot_qcs])]
341+
336342
else:
337343
backend_obj_list = pot_qcs
338344
err_cnt = None
339-
345+
346+
if return_tensors:
347+
return backend_obj_list, err_cnt, tensors
340348
return backend_obj_list, err_cnt

genQC/platform/backends/circuits_cudaq.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,10 @@ def genqc_to_backend(self,
128128
instruction.params if instruction.params else torch.nan
129129
for instruction in instructions.data
130130
]) # ... [seq, nP]
131-
132-
assert _params.shape[1] == 1 #only support nP=1 for now
133-
_params = _params.squeeze()
131+
132+
if not torch.isnan(_params).any():
133+
assert _params.shape[1] == 1 #only support nP=1 for now
134+
_params = _params.squeeze()
134135

135136
#--------------------
136137

settings.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
### Python library ###
44
repo = genQC
55
lib_name = %(repo)s
6-
version = 0.2.1
6+
version = 0.2.2
77
min_python = 3.12
88
license = apache2
99
black_formatting = False

src/examples/Quantum circuit synthesis with diffusion models/2_unitary_compilation.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
"metadata": {},
9999
"outputs": [],
100100
"source": [
101-
"pipeline = DiffusionPipeline.from_pretrained(\"Floki00/qc_unitary_3qubit\", device, )"
101+
"pipeline = DiffusionPipeline.from_pretrained(\"Floki00/qc_unitary_3qubit\", device)"
102102
]
103103
},
104104
{

src/inference/sampling.ipynb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,9 @@
399399
" params: Optional[torch.Tensor] = None, \n",
400400
" silent: bool = True,\n",
401401
" n_jobs: int = 1,\n",
402-
" filter_errs: bool = True) -> tuple[Sequence[any], int]:\n",
402+
" filter_errs: bool = True,\n",
403+
" return_tensors: bool = False,\n",
404+
" ) -> tuple[Sequence[any], int] | tuple[Sequence[any], int, torch.Tensor]:\n",
403405
" tensors = tensors.cpu()\n",
404406
"\n",
405407
" if exists(params):\n",
@@ -426,10 +428,16 @@
426428
" backend_obj_list = [pot_qc for pot_qc in pot_qcs if exists(pot_qc)]\n",
427429
" err_cnt = sum(1 for pot_qc in pot_qcs if not_exists(pot_qc))\n",
428430
" assert len(backend_obj_list) + err_cnt == len(pot_qcs)\n",
431+
"\n",
432+
" if return_tensors:\n",
433+
" tensors = tensors[torch.tensor([exists(pot_qc) for pot_qc in pot_qcs])]\n",
434+
" \n",
429435
" else:\n",
430436
" backend_obj_list = pot_qcs\n",
431437
" err_cnt = None\n",
432-
" \n",
438+
"\n",
439+
" if return_tensors:\n",
440+
" return backend_obj_list, err_cnt, tensors\n",
433441
" return backend_obj_list, err_cnt"
434442
]
435443
},

src/platform/backends/circuits_cudaq.ipynb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,10 @@
182182
" instruction.params if instruction.params else torch.nan \n",
183183
" for instruction in instructions.data\n",
184184
" ]) # ... [seq, nP]\n",
185-
" \n",
186-
" assert _params.shape[1] == 1 #only support nP=1 for now\n",
187-
" _params = _params.squeeze()\n",
185+
"\n",
186+
" if not torch.isnan(_params).any():\n",
187+
" assert _params.shape[1] == 1 #only support nP=1 for now\n",
188+
" _params = _params.squeeze()\n",
188189
"\n",
189190
" #--------------------\n",
190191
" \n",
@@ -332,7 +333,7 @@
332333
"q2 : ┤ ry(0.1) ├──●───╳───────────────────────\n",
333334
" ╰─────────╯ \n",
334335
"\n",
335-
"Measurement distribution:{ 000:81 010:413 100:84 101:1 110:421 }\n",
336+
"Measurement distribution:{ 000:85 010:401 100:85 110:429 }\n",
336337
"\n"
337338
]
338339
}
@@ -424,8 +425,8 @@
424425
"name": "stdout",
425426
"output_type": "stream",
426427
"text": [
427-
"Timeit target='qpp-cpu': 1.14 ms ± 63.4 μs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)\n",
428-
"Timeit target='nvidia': 157 ms ± 24 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
428+
"Timeit target='qpp-cpu': 1.08 ms ± 58.9 μs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)\n",
429+
"Timeit target='nvidia': 13.5 ms ± 3.14 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
429430
]
430431
}
431432
],

0 commit comments

Comments
 (0)