Skip to content

Commit 33396f3

Browse files
committed
Revert "partial fix qiskit 1.4 version"
This reverts commit 267b328. revert back to init state
1 parent 267b328 commit 33396f3

File tree

5 files changed

+280
-339
lines changed

5 files changed

+280
-339
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ repos:
4040
rev: 22.12.0
4141
hooks:
4242
- id: black
43-
language_version: python3.12
43+
language_version: python3.8
4444

4545
- repo: https://github.com/PyCQA/flake8
4646
rev: 6.0.0

torchquantum/plugin/qiskit/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@
2424

2525
from .qiskit_macros import *
2626
from .qiskit_plugin import *
27+
from .qiskit_pulse import *
2728
from .qiskit_processor import *
2829
from .qiskit_unitary_gate import *

torchquantum/plugin/qiskit/qiskit_plugin.py

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,10 @@
2727
import torchquantum.functional as tqf
2828
import qiskit.circuit.library.standard_gates as qiskit_gate
2929
import numpy as np
30-
import re
3130

32-
import qiskit
3331
from qiskit import QuantumCircuit, ClassicalRegister
34-
from qiskit_aer import AerSimulator, UnitarySimulator
35-
from qiskit import transpile
32+
from qiskit import Aer, execute
3633
from qiskit.circuit import Parameter
37-
from qiskit.circuit.library import UnitaryGate
3834
from torchpack.utils.logging import logger
3935
from torchquantum.util import (
4036
switch_little_big_endian_matrix,
@@ -89,9 +85,7 @@ def qiskit2tq_op_history(circ):
8985
init_params = (
9086
list(map(float, gate[0].params)) if len(gate[0].params) > 0 else None
9187
)
92-
print(
93-
op_name,
94-
)
88+
print(op_name,)
9589

9690
if op_name in [
9791
"h",
@@ -110,12 +104,12 @@ def qiskit2tq_op_history(circ):
110104
]:
111105
ops.append(
112106
{
113-
"name": op_name, # type: ignore
114-
"wires": np.array(wires),
115-
"params": None,
116-
"inverse": False,
117-
"trainable": False,
118-
}
107+
"name": op_name, # type: ignore
108+
"wires": np.array(wires),
109+
"params": None,
110+
"inverse": False,
111+
"trainable": False,
112+
}
119113
)
120114
elif op_name in [
121115
"rx",
@@ -144,13 +138,12 @@ def qiskit2tq_op_history(circ):
144138
]:
145139
ops.append(
146140
{
147-
"name": op_name, # type: ignore
148-
"wires": np.array(wires),
149-
"params": init_params,
150-
"inverse": False,
151-
"trainable": True,
152-
}
153-
)
141+
"name": op_name, # type: ignore
142+
"wires": np.array(wires),
143+
"params": init_params,
144+
"inverse": False,
145+
"trainable": True
146+
})
154147
elif op_name in ["barrier", "measure"]:
155148
continue
156149
else:
@@ -213,10 +206,7 @@ def append_parameterized_gate(func, circ, input_idx, params, wires):
213206
)
214207
elif func == "u2":
215208
from qiskit.circuit.library import U2Gate
216-
217-
circ.append(
218-
U2Gate(phi=params[input_idx[0]], lam=params[input_idx[1]]), wires, []
219-
)
209+
circ.append(U2Gate(phi=params[input_idx[0]], lam=params[input_idx[1]]), wires, [])
220210
# circ.u2(phi=params[input_idx[0]], lam=params[input_idx[1]], qubit=wires[0])
221211
elif func == "u3":
222212
circ.u(
@@ -307,7 +297,6 @@ def append_fixed_gate(circ, func, params, wires, inverse):
307297
circ.cu1(params, *wires)
308298
elif func == "u2":
309299
from qiskit.circuit.library import U2Gate
310-
311300
circ.append(U2Gate(phi=params[0], lam=params[1]), wires, [])
312301
# circ.u2(*list(params), *wires)
313302
elif func == "u3":
@@ -546,15 +535,7 @@ def tq2qiskit(
546535
circ.cu1(module.params[0][0].item(), *module.wires)
547536
elif module.name == "U2":
548537
from qiskit.circuit.library import U2Gate
549-
550-
circ.append(
551-
U2Gate(
552-
phi=module.params[0].data.cpu().numpy()[0],
553-
lam=module.params[0].data.cpu().numpy()[0],
554-
),
555-
module.wires,
556-
[],
557-
)
538+
circ.append(U2Gate(phi=module.params[0].data.cpu().numpy()[0], lam=module.params[0].data.cpu().numpy()[0]), module.wires, [])
558539
# circ.u2(*list(module.params[0].data.cpu().numpy()), *module.wires)
559540
elif module.name == "U3":
560541
circ.u3(*list(module.params[0].data.cpu().numpy()), *module.wires)
@@ -684,9 +665,11 @@ def op_history2qiskit_expand_params(n_wires, op_history, bsz):
684665
param = op["params"][i]
685666
else:
686667
param = None
687-
688-
append_fixed_gate(circ, op["name"], param, op["wires"], op["inverse"])
689-
668+
669+
append_fixed_gate(
670+
circ, op["name"], param, op["wires"], op["inverse"]
671+
)
672+
690673
circs_all.append(circ)
691674

692675
return circs_all
@@ -779,7 +762,7 @@ def qiskit2tq_Operator(circ: QuantumCircuit):
779762
raise NotImplementedError(
780763
f"{op_name} conversion to tq is currently not supported."
781764
)
782-
765+
783766
return ops
784767

785768

torchquantum/plugin/qiskit/qiskit_processor.py

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626
import torchquantum as tq
2727
import pathos.multiprocessing as multiprocessing
2828
import itertools
29-
import warnings # Added for handling deprecation warnings
3029

31-
from qiskit import transpile, QuantumCircuit
32-
from qiskit_aer.noise import NoiseModel
30+
from qiskit import Aer, execute, IBMQ, transpile, QuantumCircuit
31+
from qiskit.providers.aer.noise import NoiseModel
32+
from qiskit.tools.monitor import job_monitor
3333
from qiskit.exceptions import QiskitError
3434
from .qiskit_plugin import (
3535
tq2qiskit,
@@ -38,19 +38,19 @@
3838
)
3939
from torchquantum.util import (
4040
get_expectations_from_counts,
41-
# Removed: get_provider (IBMQ specific)
42-
# Removed: get_provider_hub_group_project (IBMQ specific)
41+
get_provider,
42+
get_provider_hub_group_project,
4343
get_circ_stats,
4444
)
45-
from .qiskit_macros import (
46-
IBMQ_NAMES,
47-
) # Keep for checking names? Or remove? Let's keep for now.
45+
from .qiskit_macros import IBMQ_NAMES
4846
from tqdm import tqdm
4947
from torchpack.utils.logging import logger
5048
from qiskit.transpiler import PassManager
5149
import numpy as np
5250
import datetime
5351

52+
from .my_job_monitor import my_job_monitor
53+
5454

5555
class EmptyPassManager(PassManager):
5656
def run(self, circuits, output_name: str = None, callback=None):
@@ -758,9 +758,9 @@ def process_circs_get_joint_expval(self, circs_all, observable, parallel=True):
758758
for circ_ in circs_all:
759759
circ = circ_.copy()
760760
for k, obs in enumerate(observable):
761-
if obs == "X":
761+
if obs == 'X':
762762
circ.h(k)
763-
elif obs == "Y":
763+
elif obs == 'Y':
764764
circ.z(k)
765765
circ.s(k)
766766
circ.h(k)
@@ -771,10 +771,8 @@ def process_circs_get_joint_expval(self, circs_all, observable, parallel=True):
771771

772772
mask = np.ones(len(observable), dtype=bool)
773773
mask[np.array([*observable]) == "I"] = False
774-
775-
counts = self.process_ready_circs_get_counts(
776-
circs_all_diagonalized, parallel=parallel
777-
)
774+
775+
counts = self.process_ready_circs_get_counts(circs_all_diagonalized, parallel=parallel)
778776

779777
# here we need to switch the little and big endian of distribution bitstrings
780778
distributions = []
@@ -788,35 +786,31 @@ def process_circs_get_joint_expval(self, circs_all, observable, parallel=True):
788786
n_eigen_one = 0
789787
n_eigen_minus_one = 0
790788
for bitstring, n_count in distri.items():
791-
if (
792-
np.dot(list(map(lambda x: eval(x), [*bitstring])), mask).sum() % 2
793-
== 0
794-
):
789+
if np.dot(list(map(lambda x: eval(x), [*bitstring])), mask).sum() % 2 == 0:
795790
n_eigen_one += n_count
796791
else:
797792
n_eigen_minus_one += n_count
798-
799-
expval = (
800-
n_eigen_one / self.n_shots + (-1) * n_eigen_minus_one / self.n_shots
801-
)
793+
794+
expval = n_eigen_one / self.n_shots + (-1) * n_eigen_minus_one / self.n_shots
802795
expval_all.append(expval)
803796

804797
return expval_all
805798

806799

807-
if __name__ == "__main__":
800+
if __name__ == '__main__':
808801
import pdb
809-
810802
pdb.set_trace()
811803
circ = QuantumCircuit(3)
812804
circ.h(0)
813805
circ.cx(0, 1)
814806
circ.cx(1, 2)
815807
circ.rx(0.1, 0)
816808

817-
qiskit_processor = QiskitProcessor(use_real_qc=False)
809+
qiskit_processor = QiskitProcessor(
810+
use_real_qc=False
811+
)
818812

819-
qiskit_processor.process_circs_get_joint_expval([circ], "XII")
813+
qiskit_processor.process_circs_get_joint_expval([circ], 'XII')
820814

821815
qdev = tq.QuantumDevice(n_wires=3, bsz=1)
822816
qdev.h(0)
@@ -825,5 +819,5 @@ def process_circs_get_joint_expval(self, circs_all, observable, parallel=True):
825819
qdev.rx(0, 0.1)
826820

827821
from torchquantum.measurement import expval_joint_sampling
822+
print(expval_joint_sampling(qdev, 'XII', n_shots=8192))
828823

829-
print(expval_joint_sampling(qdev, "XII", n_shots=8192))

0 commit comments

Comments
 (0)