Skip to content

Commit

Permalink
Accept FSim(pi/2, pi/6) in ConvertToSycamoreGates (#2788)
Browse files Browse the repository at this point in the history
Currently the optimizer accepts the `SycamoreGate` specifically, but fails on an equivalent `FSimGate`. This adds support for fsim gates with sycamore parameters (theta = pi/2, phi = ph/6).

Also adds support for PhasedXZGate as a native gate that will be passed through when optimizing for sycamore.
  • Loading branch information
maffoo committed Feb 25, 2020
1 parent 875132c commit 3a6ad8d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
18 changes: 14 additions & 4 deletions cirq/google/optimizers/convert_to_sycamore_gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,20 @@ def _is_native_sycamore_op(self, op: ops.Operation) -> bool:
Returns:
True if the operation is native to the gmon, false otherwise.
"""
return (isinstance(op, ops.GateOperation) and isinstance(
cast(ops.GateOperation, op).gate,
(SycamoreGate, ops.MeasurementGate, ops.PhasedXPowGate,
ops.XPowGate, ops.YPowGate, ops.ZPowGate)))
gate = op.gate

if isinstance(
gate,
(SycamoreGate, ops.MeasurementGate, ops.PhasedXZGate,
ops.PhasedXPowGate, ops.XPowGate, ops.YPowGate, ops.ZPowGate)):
return True

if (isinstance(gate, ops.FSimGate) and
math.isclose(gate.theta, np.pi / 2) and
math.isclose(gate.phi, np.pi / 6)):
return True

return False

def _convert_one(self, op: ops.Operation) -> ops.OP_TREE:
"""
Expand Down
23 changes: 23 additions & 0 deletions cirq/google/optimizers/convert_to_sycamore_gates_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ def test_convert_to_sycamore_gates_swap_zz():
circuit1, compiled_circuit1, atol=1e-7)


def test_convert_to_sycamore_gates_fsim():
q0, q1 = cirq.LineQubit.range(2)
circuit = cirq.Circuit(
cirq.FSimGate(theta=np.pi / 2, phi=np.pi / 6)(q0, q1))
compiled_circuit = circuit.copy()
cgoc.ConvertToSycamoreGates()(compiled_circuit)

cirq.testing.assert_same_circuits(circuit, compiled_circuit)


def test_single_qubit_gate():
q = cirq.LineQubit(0)
mat = cirq.testing.random_unitary(2)
Expand All @@ -52,6 +62,19 @@ def test_single_qubit_gate():
circuit, converted_circuit, atol=1e-8)


def test_single_qubit_gate_phased_xz():
q = cirq.LineQubit(0)
gate = cirq.PhasedXZGate(axis_phase_exponent=0.2,
x_exponent=0.3,
z_exponent=0.4)
circuit = cirq.Circuit(gate(q))
converted_circuit = circuit.copy()
cgoc.ConvertToSycamoreGates().optimize_circuit(converted_circuit)
ops = list(converted_circuit.all_operations())
assert len(ops) == 1
assert ops[0].gate == gate


def test_unsupported_gate():

class UnknownGate(cirq.TwoQubitGate):
Expand Down

0 comments on commit 3a6ad8d

Please sign in to comment.