Example #1
0
def test_json():
    q0, q1 = cirq.GridQubit.rect(1, 2)
    qtrit = cirq.GridQid(1, 2, dimension=3)
    cirq.testing.assert_json_roundtrip_works(cirq.wait(q0, nanos=10))
    cirq.testing.assert_json_roundtrip_works(cirq.wait(q0, q1, nanos=10))
    cirq.testing.assert_json_roundtrip_works(cirq.wait(qtrit, nanos=10))
    cirq.testing.assert_json_roundtrip_works(cirq.wait(qtrit, q1, nanos=10))
def test_cpmg_circuit():
    """Tests sub-component to make sure CPMG circuit is generated correctly."""
    q = cirq.GridQubit(1, 1)
    t = sympy.Symbol('t')
    circuit = t2._cpmg_circuit(q, t, 2)
    expected = cirq.Circuit(
        cirq.Y(q)**0.5, cirq.wait(q, nanos=t), cirq.X(q),
        cirq.wait(q, nanos=2 * t * sympy.Symbol('pulse_0')),
        cirq.X(q)**sympy.Symbol('pulse_0'),
        cirq.wait(q, nanos=2 * t * sympy.Symbol('pulse_1')),
        cirq.X(q)**sympy.Symbol('pulse_1'), cirq.wait(q, nanos=t))
    assert circuit == expected
def test_adding_gates_multiple_times():
    # Deprecations: cirq_google.SerializableDevice, cirq_google.SerializableGateSet,
    # common serializers, and
    # cirq_google.devices.known_devices.create_device_proto_from_diagram
    with cirq.testing.assert_deprecated(
            'Use cirq_google.GridDevice',
            'SerializableGateSet',
            'CircuitSerializer',
            'no longer be available',
            deadline='v0.16',
            count=11,
    ):
        waiting_for_godot = cirq_google.SerializableGateSet(
            gate_set_name='wait_gateset',
            serializers=[
                cgc.WAIT_GATE_SERIALIZER,
                cgc.WAIT_GATE_SERIALIZER,
                cgc.WAIT_GATE_SERIALIZER,
            ],
            deserializers=[
                cgc.WAIT_GATE_DESERIALIZER,
                cgc.WAIT_GATE_DESERIALIZER,
                cgc.WAIT_GATE_DESERIALIZER,
            ],
        )
        wait_proto = cirq_google.devices.known_devices.create_device_proto_from_diagram(
            "aa", [waiting_for_godot])
        wait_device = cirq_google.SerializableDevice.from_proto(
            proto=wait_proto, gate_sets=[waiting_for_godot])
        q0 = cirq.GridQubit(0, 0)
        wait_op = cirq.wait(q0, nanos=25)
        wait_device.validate_operation(wait_op)

        assert (str(wait_proto) == """\
valid_gate_sets {
  name: "wait_gateset"
  valid_gates {
    id: "wait"
    number_of_qubits: 1
    valid_args {
      name: "nanos"
      type: FLOAT
    }
  }
}
valid_qubits: "0_0"
valid_qubits: "0_1"
valid_targets {
  name: "meas_targets"
  target_ordering: SUBSET_PERMUTATION
}
valid_targets {
  name: "2_qubit_targets"
  target_ordering: SYMMETRIC
  targets {
    ids: "0_0"
    ids: "0_1"
  }
}
""")
def test_noise_from_wait():
    # Verify that wait-gate noise is duration-dependent.
    q0 = cirq.LineQubit(0)
    gate_durations = {cirq.ZPowGate: 25.0}
    heat_rate_GHz = {q0: 1e-5}
    cool_rate_GHz = {q0: 1e-4}
    model = ThermalNoiseModel(
        qubits={q0},
        gate_durations_ns=gate_durations,
        heat_rate_GHz=heat_rate_GHz,
        cool_rate_GHz=cool_rate_GHz,
        dephase_rate_GHz=None,
        require_physical_tag=False,
        skip_measurements=True,
    )
    moment = cirq.Moment(cirq.wait(q0, nanos=100))
    noisy_moment = model.noisy_moment(moment, system_qubits=[q0])
    assert noisy_moment[0] == moment
    assert len(noisy_moment[1]) == 1
    noisy_choi = cirq.kraus_to_choi(cirq.kraus(noisy_moment[1].operations[0]))
    print(noisy_choi)
    assert np.allclose(
        noisy_choi,
        [
            [9.99005480e-01, 0, 0, 9.94515097e-01],
            [0, 9.94520111e-03, 0, 0],
            [0, 0, 9.94520111e-04, 0],
            [9.94515097e-01, 0, 0, 9.90054799e-01],
        ],
    )
Example #5
0
def test_wait_gate_multi_qubit():
    # Deprecations: cirq_google.SerializableGateSet and common serializers.
    with cirq.testing.assert_deprecated('SerializableGateSet',
                                        'CircuitSerializer',
                                        deadline='v0.16',
                                        count=3):
        gate_set = cg.SerializableGateSet('test', [cgc.WAIT_GATE_SERIALIZER],
                                          [cgc.WAIT_GATE_DESERIALIZER])
        proto = op_proto({
            'gate': {
                'id': 'wait'
            },
            'args': {
                'nanos': {
                    'arg_value': {
                        'float_value': 20.0
                    }
                }
            },
            'qubits': [{
                'id': '1_2'
            }, {
                'id': '3_4'
            }],
        })
        op = cirq.wait(cirq.GridQubit(1, 2), cirq.GridQubit(3, 4), nanos=20)
        assert gate_set.serialize_op(op) == proto
        assert gate_set.deserialize_op(proto) == op
Example #6
0
def test_wait_gates():
    q0 = cirq.LineQubit(0)
    props = sample_noise_properties([q0], [])
    model = NoiseModelFromGoogleNoiseProperties(props)
    op = cirq.wait(q0, nanos=100)
    circuit = cirq.Circuit(op)
    noisy_circuit = circuit.with_noise(model)
    assert len(noisy_circuit.moments) == 2
    assert noisy_circuit.moments[0].operations[0] == op.with_tags(PHYSICAL_GATE_TAG)

    # No depolarizing noise because WaitGate has none.

    assert len(noisy_circuit.moments[1].operations) == 1
    thermal_op = noisy_circuit.moments[1].operations[0]
    assert isinstance(thermal_op.gate, cirq.KrausChannel)
    thermal_choi = cirq.kraus_to_choi(cirq.kraus(thermal_op))
    assert np.allclose(
        thermal_choi,
        [
            [1, 0, 0, 9.990005e-01],
            [0, 9.99500167e-04, 0, 0],
            [0, 0, 0, 0],
            [9.990005e-01, 0, 0, 9.990005e-01],
        ],
    )
def test_proto_with_waitgate():
    wait_gateset = cirq_google.serializable_gate_set.SerializableGateSet(
        gate_set_name='wait_gateset',
        serializers=[cgc.WAIT_GATE_SERIALIZER],
        deserializers=[cgc.WAIT_GATE_DESERIALIZER],
    )
    wait_proto = cirq_google.devices.known_devices.create_device_proto_from_diagram(
        "aa\naa",
        [wait_gateset],
    )
    wait_device = cirq_google.SerializableDevice.from_proto(
        proto=wait_proto, gate_sets=[wait_gateset])
    q0 = cirq.GridQubit(1, 1)
    wait_op = cirq.wait(q0, nanos=25)
    wait_device.validate_operation(wait_op)

    assert (str(wait_proto) == """\
valid_gate_sets {
  name: "wait_gateset"
  valid_gates {
    id: "wait"
    number_of_qubits: 1
    valid_args {
      name: "nanos"
      type: FLOAT
    }
  }
}
valid_qubits: "0_0"
valid_qubits: "0_1"
valid_qubits: "1_0"
valid_qubits: "1_1"
valid_targets {
  name: "meas_targets"
  target_ordering: SUBSET_PERMUTATION
}
valid_targets {
  name: "2_qubit_targets"
  target_ordering: SYMMETRIC
  targets {
    ids: "0_0"
    ids: "0_1"
  }
  targets {
    ids: "0_0"
    ids: "1_0"
  }
  targets {
    ids: "0_1"
    ids: "1_1"
  }
  targets {
    ids: "1_0"
    ids: "1_1"
  }
}
""")
Example #8
0
def test_final_state_vector_is_not_last_object():
    sim = cirq.Simulator()

    q = cirq.LineQubit(0)
    initial_state = np.array([1, 0], dtype=np.complex64)
    circuit = cirq.Circuit(cirq.wait(q))
    result = sim.simulate(circuit, initial_state=initial_state)
    assert result.state_vector() is not initial_state
    assert not np.shares_memory(result.state_vector(), initial_state)
    np.testing.assert_equal(result.state_vector(), initial_state)
Example #9
0
def test_final_density_matrix_is_not_last_object():
    sim = cirq.DensityMatrixSimulator()

    q = cirq.LineQubit(0)
    initial_state = np.array([[1, 0], [0, 0]], dtype=np.complex64)
    circuit = cirq.Circuit(cirq.wait(q))
    result = sim.simulate(circuit, initial_state=initial_state)
    assert result.final_density_matrix is not initial_state
    assert not np.shares_memory(result.final_density_matrix, initial_state)
    np.testing.assert_equal(result.final_density_matrix, initial_state)
Example #10
0
def test_symbolic_times_for_wait_gate():
    q0 = cirq.LineQubit(0)
    gate_durations = {cirq.ZPowGate: 25.0}
    heat_rate_GHz = {q0: 1e-5}
    cool_rate_GHz = {q0: 1e-4}
    model = ThermalNoiseModel(
        qubits={q0},
        gate_durations_ns=gate_durations,
        heat_rate_GHz=heat_rate_GHz,
        cool_rate_GHz=cool_rate_GHz,
        dephase_rate_GHz=None,
        require_physical_tag=False,
        skip_measurements=True,
    )
    moment = cirq.Moment(cirq.wait(q0, nanos=sympy.Symbol('t')))
    with pytest.raises(ValueError, match='Symbolic'):
        _ = model.noisy_moment(moment, system_qubits=[q0])
Example #11
0
def test_proto_with_waitgate():
    wait_gateset = cg.serializable_gate_set.SerializableGateSet(
        gate_set_name='wait_gateset',
        serializers=[cgc.WAIT_GATE_SERIALIZER],
        deserializers=[cgc.WAIT_GATE_DESERIALIZER],
    )
    wait_proto = cg.devices.known_devices.create_device_proto_from_diagram(
        "aa\naa",
        [wait_gateset],
    )
    wait_device = cg.SerializableDevice.from_proto(proto=wait_proto,
                                                   gate_sets=[wait_gateset])
    q0 = cirq.GridQubit(1, 1)
    wait_op = cirq.wait(q0, nanos=25)
    wait_device.validate_operation(wait_op)

    assert str(wait_proto) == """\
def test_serialize_deserialize_wait_gate():
    op = cirq.wait(cirq.GridQubit(1, 2), nanos=50.0)
    proto = op_proto({
        'gate': {
            'id': 'wait'
        },
        'qubits': [{
            'id': '1_2'
        }],
        'args': {
            'nanos': {
                'arg_value': {
                    'float_value': 50.0
                }
            },
        },
    })
    assert cg.SYC_GATESET.serialize_op(op) == proto
    assert cg.SYC_GATESET.deserialize_op(proto) == op
    assert cg.SQRT_ISWAP_GATESET.serialize_op(op) == proto
    assert cg.SQRT_ISWAP_GATESET.deserialize_op(proto) == op
Example #13
0
def _all_operations(q0, q1, q2, q3, q4, include_measurements=True):
    return (
        cirq.Z(q0),
        cirq.Z(q0)**0.625,
        cirq.Y(q0),
        cirq.Y(q0)**0.375,
        cirq.X(q0),
        cirq.X(q0)**0.875,
        cirq.H(q1),
        cirq.CZ(q0, q1),
        cirq.CZ(q0, q1)**0.25,  # Requires 2-qubit decomposition
        cirq.CNOT(q0, q1),
        cirq.CNOT(q0, q1)**0.5,  # Requires 2-qubit decomposition
        cirq.SWAP(q0, q1),
        cirq.SWAP(q1, q0)**-1,
        cirq.SWAP(q0, q1)**0.75,  # Requires 2-qubit decomposition
        cirq.CCZ(q0, q1, q2),
        cirq.CCX(q0, q1, q2),
        cirq.CCZ(q0, q1, q2)**0.5,
        cirq.CCX(q0, q1, q2)**0.5,
        cirq.CSWAP(q0, q1, q2),
        cirq.XX(q0, q1),
        cirq.XX(q0, q1)**0.75,
        cirq.YY(q0, q1),
        cirq.YY(q0, q1)**0.75,
        cirq.ZZ(q0, q1),
        cirq.ZZ(q0, q1)**0.75,
        cirq.IdentityGate(1).on(q0),
        cirq.IdentityGate(3).on(q0, q1, q2),
        cirq.ISWAP(q2, q0),  # Requires 2-qubit decomposition
        cirq.PhasedXPowGate(phase_exponent=0.111, exponent=0.25).on(q1),
        cirq.PhasedXPowGate(phase_exponent=0.333, exponent=0.5).on(q1),
        cirq.PhasedXPowGate(phase_exponent=0.777, exponent=-0.5).on(q1),
        cirq.wait(q0, nanos=0),
        cirq.measure(q0, key='xX'),
        cirq.measure(q2, key='x_a'),
        cirq.measure(q3, key='X'),
        cirq.measure(q2, key='x_a'),
        cirq.measure(q1, q2, q3, key='multi', invert_mask=(False, True)),
    )
def test_wait_gate():
    gate_set = cg.SerializableGateSet('test', [cgc.WAIT_GATE_SERIALIZER],
                                      [cgc.WAIT_GATE_DESERIALIZER])
    proto = op_proto({
        'gate': {
            'id': 'wait'
        },
        'args': {
            'nanos': {
                'arg_value': {
                    'float_value': 20.0
                }
            }
        },
        'qubits': [{
            'id': '1_2'
        }],
    })
    q = cirq.GridQubit(1, 2)
    op = cirq.wait(q, nanos=20)
    assert gate_set.serialize_op(op) == proto
    assert gate_set.deserialize_op(proto) == op
Example #15
0
def test_adding_gates_multiple_times():
    waiting_for_godot = cg.serializable_gate_set.SerializableGateSet(
        gate_set_name='wait_gateset',
        serializers=[
            cgc.WAIT_GATE_SERIALIZER, cgc.WAIT_GATE_SERIALIZER,
            cgc.WAIT_GATE_SERIALIZER
        ],
        deserializers=[
            cgc.WAIT_GATE_DESERIALIZER, cgc.WAIT_GATE_DESERIALIZER,
            cgc.WAIT_GATE_DESERIALIZER
        ],
    )
    wait_proto = cg.devices.known_devices.create_device_proto_from_diagram(
        "aa",
        [waiting_for_godot],
    )
    wait_device = cg.SerializableDevice.from_proto(
        proto=wait_proto, gate_sets=[waiting_for_godot])
    q0 = cirq.GridQubit(0, 0)
    wait_op = cirq.wait(q0, nanos=25)
    wait_device.validate_operation(wait_op)

    assert str(wait_proto) == """\
Example #16
0
def test_protocols():
    t = sympy.Symbol('t')
    p = cirq.WaitGate(cirq.Duration(millis=5 * t))
    c = cirq.WaitGate(cirq.Duration(millis=2))
    q = cirq.LineQubit(0)

    cirq.testing.assert_implements_consistent_protocols(cirq.wait(q, nanos=0))
    cirq.testing.assert_implements_consistent_protocols(c.on(q))
    cirq.testing.assert_implements_consistent_protocols(p.on(q))

    assert cirq.has_unitary(p)
    assert cirq.has_unitary(c)
    assert cirq.is_parameterized(p)
    assert not cirq.is_parameterized(c)
    assert cirq.resolve_parameters(p, {'t': 2}) == cirq.WaitGate(cirq.Duration(millis=10))
    assert cirq.resolve_parameters(c, {'t': 2}) == c
    assert cirq.resolve_parameters_once(c, {'t': 2}) == c
    assert cirq.trace_distance_bound(p) == 0
    assert cirq.trace_distance_bound(c) == 0
    assert cirq.inverse(c) == c
    assert cirq.inverse(p) == p
    assert cirq.decompose(c.on(q)) == []
    assert cirq.decompose(p.on(q)) == []
Example #17
0
def test_serialize_deserialize_wait_gate():
    with cirq.testing.assert_deprecated('SerializableGateSet',
                                        deadline='v0.16',
                                        count=4):
        op = cirq.wait(cirq.GridQubit(1, 2), nanos=50.0)
        proto = op_proto({
            'gate': {
                'id': 'wait'
            },
            'qubits': [{
                'id': '1_2'
            }],
            'args': {
                'nanos': {
                    'arg_value': {
                        'float_value': 50.0
                    }
                }
            },
        })
        assert cg.SYC_GATESET.serialize_op(op) == proto
        assert cg.SYC_GATESET.deserialize_op(proto) == op
        assert cg.SQRT_ISWAP_GATESET.serialize_op(op) == proto
        assert cg.SQRT_ISWAP_GATESET.deserialize_op(proto) == op