Skip to content

Commit 3364975

Browse files
committed
create a new branch for upgrading to qiskit 1.3
1 parent 611cc2a commit 3364975

File tree

9 files changed

+63
-17
lines changed

9 files changed

+63
-17
lines changed

.cursorignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Add directories or file patterns to ignore during indexing (e.g. foo/ or *.csv)
2+
docs/
3+
examples/
4+
figs/

examples/QuantumNAS/quantumnas.ipynb

+3-2
Original file line numberDiff line numberDiff line change
@@ -4056,7 +4056,8 @@
40564056
"toc_visible": true
40574057
},
40584058
"kernelspec": {
4059-
"display_name": "Python 3",
4059+
"display_name": "torchquantum",
4060+
"language": "python",
40604061
"name": "python3"
40614062
},
40624063
"language_info": {
@@ -4069,7 +4070,7 @@
40694070
"name": "python",
40704071
"nbconvert_exporter": "python",
40714072
"pygments_lexer": "ipython3",
4072-
"version": "3.8.16"
4073+
"version": "3.9.20"
40734074
},
40744075
"widgets": {
40754076
"application/vnd.jupyter.widget-state+json": {

test/qiskit_plugin_test.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
get_expectations_from_counts,
3737
find_global_phase,
3838
)
39-
from test.static_mode_test import QLayer as AllRandomLayer
39+
from static_mode_test import QLayer as AllRandomLayer
4040
from torchquantum.plugin import tq2qiskit
4141
from torchquantum.macro import F_DTYPE
4242

@@ -54,11 +54,12 @@ def unitary_tq_vs_qiskit_test():
5454
qiskit_compatible=True,
5555
)
5656

57-
unitary_tq = q_layer.get_unitary(q_dev, x)
57+
# unitary_tq = q_layer.get_unitary(q_dev, x)
58+
unitary_tq = q_layer.get_unitary(x)
5859
unitary_tq = switch_little_big_endian_matrix(unitary_tq.data.numpy())
5960

6061
# qiskit
61-
circ = tq2qiskit(q_layer, x)
62+
circ = tq2qiskit(q_dev, q_layer, x)
6263
simulator = Aer.get_backend("unitary_simulator")
6364
result = execute(circ, simulator).result()
6465
unitary_qiskit = result.get_unitary(circ)

test/static_mode_test.py

+17-3
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,20 @@ def build_random_funcs(self):
155155
cnt = 0
156156
while cnt < self.n_funcs:
157157
func = np.random.choice(self.funcs)
158-
n_func_wires = op_name_dict[func]().num_wires
158+
# print(f"Selected function: {func}")
159+
160+
"""
161+
ORIGINAL: n_func_wires = op_name_dict[func]().num_wires
162+
Changed to avoid initialization error with QubitUnitaryFast which requires
163+
parameters during instantiation. Instead, we access num_wires directly
164+
from the class since it's a class attribute.
165+
"""
166+
167+
op_class = op_name_dict[func]
168+
# print(f"Operator class: {op_class}")
169+
# print(f"Number of wires: {op_class.num_wires}")
170+
n_func_wires = op_class.num_wires
171+
159172
if n_func_wires > self.n_wires:
160173
continue
161174
cnt += 1
@@ -191,8 +204,9 @@ def forward(self, q_device: tq.QuantumDevice, x):
191204
self.func_list, self.func_wires_list, self.func_inverse
192205
):
193206
n_func_wires = len(func_wires)
194-
n_func_params = op_name_dict[func]().num_params
195-
207+
op_class = op_name_dict[func]
208+
# n_func_params = op_name_dict[func]().num_params
209+
n_func_params = op_class.num_params
196210
if n_func_params == 0:
197211
if func in ["multicnot", "multixcnot"]:
198212
func_name_dict[func](

torchquantum/functional/u1.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def u1(
110110
111111
"""
112112
name = "u1"
113-
mat = mat_dict[name]
113+
mat = _u1_mat_dict[name]
114114
gate_wrapper(
115115
name=name,
116116
mat=mat,
@@ -157,7 +157,7 @@ def cu1(
157157
158158
"""
159159
name = "cu1"
160-
mat = mat_dict[name]
160+
mat = _u1_mat_dict[name]
161161
gate_wrapper(
162162
name=name,
163163
mat=mat,

torchquantum/functional/u2.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def u2(
109109
110110
"""
111111
name = "u2"
112-
mat = mat_dict[name]
112+
mat = _u2_mat_dict[name]
113113
gate_wrapper(
114114
name=name,
115115
mat=mat,
@@ -156,7 +156,7 @@ def cu2(
156156
157157
"""
158158
name = "cu2"
159-
mat = mat_dict[name]
159+
mat = _u2_mat_dict[name]
160160
gate_wrapper(
161161
name=name,
162162
mat=mat,

torchquantum/graph/graphs.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,17 @@ def add_func(
156156
if not self.is_list_finish:
157157
# graph construction is not finished, build a new operation and
158158
# add the operation to the graph
159-
op = tq.op_name_dict[name]()
160-
op.params = params
161-
op.n_wires = n_wires
162-
op.wires = wires
159+
print(tq.op_name_dict[name])
160+
# op = tq.op_name_dict[name]()
161+
op_class = tq.op_name_dict[name]
162+
op = op_class(has_params=True if params is not None else False,
163+
trainable=False,
164+
init_params=params,
165+
n_wires=n_wires,
166+
wires=wires)
167+
# op.params = params
168+
# op.n_wires = n_wires
169+
# op.wires = wires
163170
op.graph = tq.QuantumGraph()
164171
op.parent_graph = parent_graph
165172
op.static_mode = True

torchquantum/measurement/measurements.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,22 @@ def measure(qdev, n_shots=1024, draw_id=None):
4242
Returns:
4343
distribution of bitstrings
4444
"""
45-
bitstring_candidates = gen_bitstrings(qdev.n_wires)
45+
46+
"""
47+
In measure function, the statevector is copied to the CPU and
48+
a list of all possible 2^n bitstrings is constructed to do the sampling.
49+
This is again a huge CPU memory and runtime overhead since sampling can done on the GPU directly and efficiently.
50+
Here is a sketch of how that might look like in PyTorch using Inverse transform sampling method:
51+
Calculate squared amplitudes on GPU using troch.abs and troch.square.
52+
Calculate the cumulative distribution function using troch.cumsum.
53+
Generate random numbers (as many as the required number of samples) between 0 and 1 using torch.rand.
54+
Find the index of each random number inside the cumulative distribution using troch.searchsorted.
55+
Copy the indices to the CPU and convert each number to its bitstring binary representation.
56+
"""
57+
58+
bitstring_candidates = gen_bitstrings(qdev.n_wires) # length is 2 to the power of n_wires
4659
if isinstance(qdev, tq.QuantumDevice):
60+
# length is 2 to the power of n_wires
4761
state_mag = qdev.get_states_1d().abs().detach().cpu().numpy()
4862
elif isinstance(qdev, tq.NoiseDevice):
4963
'''

torchquantum/operator/op_types.py

+5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33
import torchquantum as tq
44
import torchquantum.functional.functionals as tqf
55
import numpy as np
6+
import logging
67
from abc import ABCMeta
78
from ..macro import C_DTYPE, F_DTYPE
89
from typing import Iterable, Union, List
910
from enum import IntEnum
1011

12+
13+
# Add logging init
14+
logger = logging.getLogger(__name__)
15+
1116
__all__ = [
1217
"Operator",
1318
"Operation",

0 commit comments

Comments
 (0)