Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9ee7950

Browse files
Merge pull request #236 from ruan-prog/master
update
2 parents df229f4 + 2032396 commit 9ee7950

File tree

9 files changed

+95
-75
lines changed

9 files changed

+95
-75
lines changed

‎tensorcircuit/__init__.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
from . import basecircuit
2727
from . import waveforms
2828
from .gates import Gate
29-
from .circuit import Circuit, expectation, Param
29+
from .circuit import Circuit, expectation
30+
from .pulse import Param
3031
from .mpscircuit import MPSCircuit
3132
from .densitymatrix import DMCircuit as DMCircuit_reference
3233
from .densitymatrix import DMCircuit2

‎tensorcircuit/circuit.py‎

Lines changed: 9 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from .quantum import QuOperator, identity
1818
from .simplify import _full_light_cone_cancel
1919
from .basecircuit import BaseCircuit
20+
from .pulse import DefcalBuilder, Param, Frame
2021

2122
Gate = gates.Gate
2223
Tensor = Any
@@ -137,21 +138,22 @@ def add_calibration(
137138
})
138139

139140

140-
def to_tqasm(self, pragma: str) -> str:
141+
def to_tqasm(self, pragma: Optional[str]=None) -> str:
141142
qasm_lines = []
142-
if pragma:
143-
qasm_lines.append(pragma)
144143

145144
qasm_lines.append("TQASM 0.2;")
146145

146+
if pragma:
147+
qasm_lines.append(pragma)
148+
147149
qasm_lines.append(f"QREG q[{self._nqubits}];")
148150

149151
for cal in getattr(self, "calibrations", []):
150152
pname = ", ".join(cal["parameters"])
151-
qasm_lines.append(f"\ndefcal {cal['name']} {pname} {{")
153+
qasm_lines.append(f"defcal {cal['name']} {pname} {{")
152154
for inst in cal["instructions"]:
153155
if inst["type"] == "frame":
154-
qasm_lines.append(f" frame {inst['frame']} = newframe({inst['qubit']});")
156+
qasm_lines.append(f" frame {inst['frame'].name} = newframe({inst['qubit']});")
155157
elif inst["type"] == "play":
156158
args_str = ", ".join(str(x) for x in inst["args"])
157159
wf_type = inst["waveform_type"]
@@ -171,7 +173,7 @@ def to_tqasm(self, pragma: str) -> str:
171173
for i, gate in enumerate(self._qir):
172174
for cal in cals_by_pos.get(i, []):
173175
# print(cal)
174-
pname = ", ".join(cal.get("parameters", []))
176+
pname = ", ".join(f"q[{x}]"forxincal.get("parameters", []))
175177
qasm_lines.append(f"{cal['name']} {pname};")
176178

177179
# print(gate)
@@ -189,7 +191,7 @@ def to_tqasm(self, pragma: str) -> str:
189191
# 收尾:把 pos == len(self._qir) 的校准放在最后
190192
for cal in cals_by_pos.get(len(self._qir), []):
191193
# print(cal)
192-
pname = ", ".join(cal.get("parameters", []))
194+
pname = ", ".join(f"q[{x}]"forxincal.get("parameters", []))
193195
qasm_lines.append(f"{cal['name']} {pname};")
194196

195197
return ("\n".join(qasm_lines))
@@ -1083,51 +1085,3 @@ def expectation(
10831085
else:
10841086
den = 1.0
10851087
return num / den
1086-
1087-
class Param:
1088-
def __init__(self, name: str):
1089-
self.name = name
1090-
1091-
class Frame:
1092-
def __init__(self, name: str):
1093-
self.name = name
1094-
1095-
class DefcalBuilder:
1096-
def __init__(self, circuit, name: str, parameters: List["Param"]):
1097-
self.circuit = circuit
1098-
self.name = name
1099-
self.parameters = parameters
1100-
self.instructions = []
1101-
1102-
def new_frame(self, frame_name: str, param: "Param"):
1103-
frame = Frame(frame_name)
1104-
self.instructions.append({
1105-
"type": "frame",
1106-
"frame": frame,
1107-
"qubit": param.name,
1108-
})
1109-
return frame
1110-
1111-
def play(self, frame: Frame, waveform: Any, start_time: int = None):
1112-
if not hasattr(waveform, "__dataclass_fields__"):
1113-
raise TypeError("Unsupported waveform type")
1114-
1115-
waveform_type = waveform.qasm_name()
1116-
args = waveform.to_args()
1117-
if start_time is not None:
1118-
args = [start_time] + args
1119-
1120-
self.instructions.append({
1121-
"type": "play",
1122-
"frame": frame.name,
1123-
"waveform_type": waveform_type,
1124-
"args": args,
1125-
})
1126-
return self
1127-
1128-
def build(self):
1129-
self.circuit.def_calibration(
1130-
name=self.name,
1131-
parameters=[p.name for p in self.parameters],
1132-
instructions=self.instructions,
1133-
)

‎tensorcircuit/cloud/__init__.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
from . import wrapper
44
from .wrapper import batch_expectation_ps
55
from .apis import submit_task
6+
from .tencent import Topology

‎tensorcircuit/cloud/tencent.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def update_pairs(user_addr1: int, user_addr2: int, add_remove: bool = True):
7676
update_pairs(user_addr2, user_addr1, add_remove)
7777
return
7878

79-
def pragmam(self) -> str:
79+
def pragma(self) -> str:
8080
lines = []
8181
if self._used_chip_qubits == [] or self._used_user_qubits == []:
8282
return None
@@ -89,7 +89,7 @@ def pragmam(self) -> str:
8989
pragma = pragma[:-2] + "]"
9090
lines.append(pragma)
9191

92-
pragma = "#pragma qubits.coupling []"
92+
pragma = "#pragma qubits.coupling ["
9393
for u1, u2 in getattr(self, "_used_user_pairs", []):
9494
pragma += f"[{u1}, {u2}], "
9595
pragma = pragma[:-2] + "]"
@@ -295,7 +295,7 @@ def c2qasm(c: Any, compiling: bool) -> str:
295295
else:
296296
prag = None
297297
if topology is not None:
298-
prag = topology.praggam()
298+
prag = topology.pragma()
299299
s = c.to_tqasm(prag)
300300
lang = "TQASM"
301301
#s = c.to_openqasm()

‎tensorcircuit/pulse.py‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from typing import List, Dict, Any
2+
3+
4+
class Param:
5+
def __init__(self, name: str):
6+
self.name = name
7+
8+
class Frame:
9+
def __init__(self, name: str):
10+
self.name = name
11+
12+
class DefcalBuilder:
13+
def __init__(self, circuit, name: str, parameters: List["Param"]):
14+
self.circuit = circuit
15+
self.name = name
16+
self.parameters = parameters
17+
self.instructions = []
18+
19+
def new_frame(self, frame_name: str, param: "Param"):
20+
frame = Frame(frame_name)
21+
self.instructions.append({
22+
"type": "frame",
23+
"frame": frame,
24+
"qubit": param.name,
25+
})
26+
return frame
27+
28+
def play(self, frame: Frame, waveform: Any, start_time: int = None):
29+
if not hasattr(waveform, "__dataclass_fields__"):
30+
raise TypeError("Unsupported waveform type")
31+
32+
waveform_type = waveform.qasm_name()
33+
args = waveform.to_args()
34+
if start_time is not None:
35+
args = [start_time] + args
36+
37+
self.instructions.append({
38+
"type": "play",
39+
"frame": frame.name,
40+
"waveform_type": waveform_type,
41+
"args": args,
42+
})
43+
return self
44+
45+
def build(self):
46+
self.circuit.def_calibration(
47+
name=self.name,
48+
parameters=[p.name for p in self.parameters],
49+
instructions=self.instructions,
50+
)

‎tests/01_test_gate.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ def gen_gate_circuit(t):
2727
qc.h(0)
2828
qc.cnot(0, 1)
2929

30+
print(qc.to_tqasm())
31+
3032
return qc
3133

3234

@@ -43,7 +45,8 @@ def run_circuit(qc):
4345
# print(qc.to_tqasm())
4446
# n = qc._nqubits
4547
rf = t.results()
46-
print(rf)
48+
# print(rf)
49+
return rf
4750

4851
qc = gen_gate_circuit(1.0)
4952
result = run_circuit(qc)

‎tests/02_test_param_pulse.py‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ def gen_parametric_waveform_circuit(t):
2727

2828
param0 = Param("a")
2929

30+
# builder = qc.calibrate("basic_pulse", [param0])
31+
# builder.new_frame("drive_frame", param0)
32+
# builder.play("drive_frame", waveforms.CosineDrag(t, 0.2, 0.0, 0.0))
33+
34+
# builder.build()
35+
# qc.add_calibration('basic_pulse', ['q[0]'])
36+
3037
# 需根据以下定义的方式,修改代码
3138
builder = qc.calibrate("basic_pulse", [param0])
3239
frame = builder.new_frame("drive_frame", param0)
@@ -36,7 +43,7 @@ def gen_parametric_waveform_circuit(t):
3643

3744
qc.add_calibration(builder, [0])
3845

39-
# print(qc.to_tqasm())
46+
print(qc.to_tqasm())
4047
return qc
4148

4249

@@ -53,7 +60,8 @@ def run_circuit(qc):
5360
# print(qc.to_tqasm())
5461
# n = qc._nqubits
5562
rf = t.results()
56-
print(rf)
63+
# print(rf)
64+
return rf
5765

5866
qc = gen_parametric_waveform_circuit(1.0)
5967
result = run_circuit(qc)

‎tests/03_test_gate_pulse_mix.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def run_circuit(qc):
5454
# print(qc.to_tqasm())
5555
# n = qc._nqubits
5656
rf = t.results()
57-
print(rf)
57+
returnrf
5858

5959
qc = gen_gate_pulse_mix_circuit(1.0)
6060
result = run_circuit(qc)

‎tests/04_test_custom_chip.py‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
sys.path.insert(0, module_path)
88

99
from tensorcircuit import Circuit, Param, gates, waveforms
10-
from tensorcircuit.cloud.apis import submit_task, get_device, set_provider, set_token, list_devices, list_properties, Topology
10+
from tensorcircuit.cloud.apis import submit_task, get_device, set_provider, set_token, list_devices, list_properties
11+
from tensorcircuit.cloud.tencent import Topology
1112
import re
1213

1314
# from dotenv import load_dotenv
@@ -22,31 +23,33 @@
2223
print(ds)
2324

2425
def gen_custom_chip_circuit(t):
25-
qc = Circuit(2)
26+
qc = Circuit(3)
2627

27-
qc.i(0)
28-
qc.cnot(0)
28+
# qc.h(0)
29+
qc.cnot(0, 2)
30+
# qc.cnot(1, 3)
31+
# qc.cnot(0, 3)
2932

3033
# print(qc.to_tqasm())
31-
return qc, tp
34+
return qc
3235

3336

3437
def run_circuit(qc):
3538
device_name = "tianji_s2"
3639
d = get_device(device_name)
3740
# 以下是两种映射方式
3841
tp = Topology(d)
39-
tp.map_qubits([4,9,8,14])
40-
tp.map_qubits([4,9,8,14], [3,2,1,0])
42+
# tp.map_qubits([6,3,4,1])
43+
# tp.map_qubits([6,3,4,1], [3,2,1,0])
4144

42-
tp.map_qubit(0, 4)
43-
tp.map_qubit(1, 9)
44-
tp.map_qubit(2, 8)
45-
tp.map_qubit(3, 14)
45+
tp.map_qubit(0, 6)
46+
tp.map_qubit(1, 3)
47+
tp.map_qubit(2, 4)
48+
tp.map_qubit(3, 1)
4649

4750
tp.pair_qubit(0, 1)
4851
tp.pair_qubit(0, 2)
49-
tp.pair_qubit(1, 3, False)
52+
tp.pair_qubit(1, 3)
5053
t = submit_task(
5154
circuit=qc,
5255
shots=shots_const,

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /