Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add warning when converting global phase operator to OpenQASM 2.0 #6473

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 30 additions & 10 deletions cirq-core/cirq/circuits/qasm_output_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,24 @@ def test_h_gate_with_parameter():
)


def test_qasm_global_pahse():
(q0,) = _make_qubits(1)
output = cirq.QasmOutput((cirq.global_phase_operation(np.exp(1j * 5)), cirq.X(q0)), (q0,))
assert (
str(output)
== """OPENQASM 2.0;
include "qelib1.inc";


// Qubits: [q0]
qreg q[1];


x q[0];
"""
)


def test_precision():
(q0,) = _make_qubits(1)
output = cirq.QasmOutput((cirq.X(q0) ** 0.1234567,), (q0,), precision=3)
Expand Down Expand Up @@ -318,16 +336,18 @@ def __repr__(self):
cirq.PhasedXPowGate(phase_exponent=0.333, exponent=0.5).on(q1),
cirq.PhasedXPowGate(phase_exponent=0.777, exponent=-0.5).on(q1),
(
cirq.measure(q0, key='xX'),
cirq.measure(q2, key='x_a'),
cirq.measure(q1, key='x?'),
cirq.measure(q3, key='X'),
cirq.measure(q4, key='_x'),
cirq.measure(q2, key='x_a'),
cirq.measure(q1, q2, q3, key='multi', invert_mask=(False, True)),
)
if include_measurements
else (),
(
cirq.measure(q0, key='xX'),
cirq.measure(q2, key='x_a'),
cirq.measure(q1, key='x?'),
cirq.measure(q3, key='X'),
cirq.measure(q4, key='_x'),
cirq.measure(q2, key='x_a'),
cirq.measure(q1, q2, q3, key='multi', invert_mask=(False, True)),
)
if include_measurements
else ()
),
ExampleOperation(),
ExampleCompositeOperation(),
)
Expand Down
11 changes: 10 additions & 1 deletion cirq-core/cirq/ops/gate_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""Basic types defining qubits, gates, and operations."""

import re
import warnings
from typing import (
AbstractSet,
Any,
Expand All @@ -35,7 +36,7 @@

import numpy as np

from cirq import protocols, value
from cirq import ops, protocols, value
from cirq.ops import raw_types, gate_features, control_values as cv
from cirq.type_workarounds import NotImplementedType

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

def _qasm_(self, args: 'protocols.QasmArgs') -> Optional[str]:
if isinstance(self.gate, ops.GlobalPhaseGate):
warnings.warn(
"OpenQASM 2.0 does not support global phase."
"Since the global phase does not affect the measurement results, "
"the conversion to QASM is disregarded."
)
return ""

return protocols.qasm(self.gate, args=args, qubits=self.qubits, default=None)

def _equal_up_to_global_phase_(
Expand Down
6 changes: 6 additions & 0 deletions cirq-core/cirq/testing/consistent_qasm.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def assert_qasm_is_consistent_with_unitary(val: Any):
if isinstance(val, ops.Operation):
qubits: Sequence[ops.Qid] = val.qubits
op = val
gate = val.gate
elif isinstance(val, ops.Gate):
qid_shape = protocols.qid_shape(val)
remaining_shape = list(qid_shape)
Expand All @@ -52,9 +53,14 @@ def assert_qasm_is_consistent_with_unitary(val: Any):
remaining_shape.pop(i)
qubits = devices.LineQid.for_qid_shape(remaining_shape)
op = val.on(*qubits)
gate = val
else:
raise NotImplementedError(f"Don't know how to test {val!r}")

if isinstance(gate, ops.GlobalPhaseGate):
# OpenQASM 2.0 does not support global phase gates.
return

args = protocols.QasmArgs(qubit_id_map={q: f'q[{i}]' for i, q in enumerate(qubits)})
qasm = protocols.qasm(op, args=args, default=None)
if qasm is None:
Expand Down
Loading