Skip to content

Commit d0fb20e

Browse files
authored
Simplify qgate_*_qasm() (#109)
* Simplify qgate_*_qasm() * lint
1 parent ce846b8 commit d0fb20e

1 file changed

Lines changed: 19 additions & 23 deletions

File tree

  • python/quantum-pecos/src/pecos/slr/gen_codes

python/quantum-pecos/src/pecos/slr/gen_codes/gen_qasm.py

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -247,38 +247,42 @@ def process_qgate(self, op):
247247
)
248248

249249
case "Prep":
250-
op_str = self.qgate_qasm(op, "reset")
250+
op_str = self.qgate_sq_qasm(op, "reset")
251251

252252
case "T":
253-
op_str = self.qgate_qasm(op, "rz(pi/4)")
253+
op_str = self.qgate_sq_qasm(op, "rz(pi/4)")
254254

255255
case "Tdg":
256-
op_str = self.qgate_qasm(op, "rz(-pi/4)")
256+
op_str = self.qgate_sq_qasm(op, "rz(-pi/4)")
257257

258258
case "SX":
259-
op_str = self.qgate_qasm(op, "rx(pi/2)")
259+
op_str = self.qgate_sq_qasm(op, "rx(pi/2)")
260260

261261
case "SY":
262-
op_str = self.qgate_qasm(op, "ry(pi/2)")
262+
op_str = self.qgate_sq_qasm(op, "ry(pi/2)")
263263

264264
case "SZ":
265-
op_str = self.qgate_qasm(op, "rz(pi/2)")
265+
op_str = self.qgate_sq_qasm(op, "rz(pi/2)")
266266

267267
case "SXdg":
268-
op_str = self.qgate_qasm(op, "rx(-pi/2)")
268+
op_str = self.qgate_sq_qasm(op, "rx(-pi/2)")
269269

270270
case "SYdg":
271-
op_str = self.qgate_qasm(op, "ry(-pi/2)")
271+
op_str = self.qgate_sq_qasm(op, "ry(-pi/2)")
272272

273273
case "SZdg":
274-
op_str = self.qgate_qasm(op, "rz(-pi/2)")
274+
op_str = self.qgate_sq_qasm(op, "rz(-pi/2)")
275275

276276
case _:
277-
op_str = self.qgate_qasm(op)
277+
op_str = self.qgate_sq_qasm(op)
278278

279279
return op_str
280280

281-
def qgate_qasm(self, op, repr_str: str | None = None):
281+
def qgate_sq_qasm(self, op, repr_str: str | None = None):
282+
if op.qsize != 1:
283+
msg = "qgate_qasm only supports single qubit gates"
284+
raise Exception(msg)
285+
282286
if repr_str is None:
283287
repr_str = op.sym.lower()
284288

@@ -288,20 +292,8 @@ def qgate_qasm(self, op, repr_str: str | None = None):
288292

289293
str_list = []
290294

291-
# TODO: Make sure this all works for different sized multi-qubit gates!!!!!!!!!!!!!!! <<<<<<<<<<
292-
293-
if op.qsize > 1:
294-
if not isinstance(op.qargs[0], tuple) and len(op.qargs) > 1:
295-
op.qargs = (op.qargs,)
296-
297295
for q in op.qargs:
298296
if isinstance(q, QReg):
299-
# Broadcasting across a qubit register is inconsistent with the current Permute
300-
# strategy, so "unroll" broadcast operations to make them act on individual qubits.
301-
# See, for example, https://github.com/PECOS-packages/PECOS/issues/95.
302-
if op.qsize != 1:
303-
msg = "Only single-qubit gates can be broadcast across a qubit register"
304-
raise Exception(msg)
305297
lines = [f"{repr_str} {qubit};" for qubit in q]
306298
str_list.extend(lines)
307299

@@ -318,6 +310,10 @@ def qgate_qasm(self, op, repr_str: str | None = None):
318310
return "\n".join(str_list)
319311

320312
def qgate_tq_qasm(self, op, repr_str: str | None = None):
313+
if op.qsize != 2:
314+
msg = "qgate_tq_qasm only supports single qubit gates"
315+
raise Exception(msg)
316+
321317
if repr_str is None:
322318
repr_str = op.sym.lower()
323319

0 commit comments

Comments
 (0)