Skip to content

Commit d75d43f

Browse files
authored
Add warning when converting global phase operator to OpenQASM 2.0 (#6476)
1 parent eef7c5c commit d75d43f

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

cirq-core/cirq/circuits/qasm_output.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,11 @@ def output(text):
293293
output(f'creg {meas_id}[{len(meas.qubits)}];\n')
294294
else:
295295
output(f'creg {meas_id}[{len(meas.qubits)}]; // Measurement: {comment}\n')
296-
output_line_gap(2)
296+
# In OpenQASM 2.0, the transformation of global phase gates is ignored.
297+
# Therefore, no newline is created when the operations contained in
298+
# a circuit consist only of global phase gates.
299+
if any(not isinstance(op.gate, ops.GlobalPhaseGate) for op in self.operations):
300+
output_line_gap(2)
297301

298302
# Operations
299303
self._write_operations(self.operations, output, output_line_gap)

cirq-core/cirq/circuits/qasm_output_test.py

+13
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,19 @@ def test_h_gate_with_parameter():
191191
)
192192

193193

194+
def test_qasm_global_pahse():
195+
output = cirq.QasmOutput((cirq.global_phase_operation(np.exp(1j * 5))), ())
196+
assert (
197+
str(output)
198+
== """OPENQASM 2.0;
199+
include "qelib1.inc";
200+
201+
202+
// Qubits: []
203+
"""
204+
)
205+
206+
194207
def test_precision():
195208
(q0,) = _make_qubits(1)
196209
output = cirq.QasmOutput((cirq.X(q0) ** 0.1234567,), (q0,), precision=3)

cirq-core/cirq/ops/gate_operation.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"""Basic types defining qubits, gates, and operations."""
1616

1717
import re
18+
import warnings
1819
from typing import (
1920
AbstractSet,
2021
Any,
@@ -35,7 +36,7 @@
3536

3637
import numpy as np
3738

38-
from cirq import protocols, value
39+
from cirq import ops, protocols, value
3940
from cirq.ops import raw_types, gate_features, control_values as cv
4041
from cirq.type_workarounds import NotImplementedType
4142

@@ -348,6 +349,14 @@ def __rmul__(self, other: Any) -> Any:
348349
return self.gate._rmul_with_qubits(self._qubits, other)
349350

350351
def _qasm_(self, args: 'protocols.QasmArgs') -> Optional[str]:
352+
if isinstance(self.gate, ops.GlobalPhaseGate):
353+
warnings.warn(
354+
"OpenQASM 2.0 does not support global phase."
355+
"Since the global phase does not affect the measurement results, "
356+
"the conversion to QASM is disregarded."
357+
)
358+
return ""
359+
351360
return protocols.qasm(self.gate, args=args, qubits=self.qubits, default=None)
352361

353362
def _equal_up_to_global_phase_(

cirq-core/cirq/testing/consistent_qasm.py

+6
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def assert_qasm_is_consistent_with_unitary(val: Any):
4242
if isinstance(val, ops.Operation):
4343
qubits: Sequence[ops.Qid] = val.qubits
4444
op = val
45+
gate = val.gate
4546
elif isinstance(val, ops.Gate):
4647
qid_shape = protocols.qid_shape(val)
4748
remaining_shape = list(qid_shape)
@@ -52,9 +53,14 @@ def assert_qasm_is_consistent_with_unitary(val: Any):
5253
remaining_shape.pop(i)
5354
qubits = devices.LineQid.for_qid_shape(remaining_shape)
5455
op = val.on(*qubits)
56+
gate = val
5557
else:
5658
raise NotImplementedError(f"Don't know how to test {val!r}")
5759

60+
if isinstance(gate, ops.GlobalPhaseGate):
61+
# OpenQASM 2.0 does not support global phase gates.
62+
return
63+
5864
args = protocols.QasmArgs(qubit_id_map={q: f'q[{i}]' for i, q in enumerate(qubits)})
5965
qasm = protocols.qasm(op, args=args, default=None)
6066
if qasm is None:

0 commit comments

Comments
 (0)