コード例 #1
0
def test_multi_qubit_measurement_to_proto():
    gate = cirq.MeasurementGate(2, 'test')
    proto = operations_pb2.Operation(measurement=operations_pb2.Measurement(
        targets=[
            operations_pb2.Qubit(row=2, col=3),
            operations_pb2.Qubit(row=3, col=4)
        ],
        key='test',
    ))
    assert_proto_dict_convert(gate, proto, cirq.GridQubit(2, 3),
                              cirq.GridQubit(3, 4))
コード例 #2
0
def test_single_qubit_measurement_to_proto_pad_invert_mask():
    gate = cirq.MeasurementGate(2, 'test', invert_mask=(True, ))
    proto = operations_pb2.Operation(measurement=operations_pb2.Measurement(
        targets=[
            operations_pb2.Qubit(row=2, col=3),
            operations_pb2.Qubit(row=2, col=4)
        ],
        key='test',
        invert_mask=[True, False],
    ))
    assert (programs.gate_to_proto(
        gate, (cirq.GridQubit(2, 3), cirq.GridQubit(2, 4)), delay=0) == proto)
コード例 #3
0
def test_w_to_proto():
    gate = cirq.PhasedXPowGate(exponent=sympy.Symbol('k'), phase_exponent=1)
    proto = operations_pb2.Operation(exp_w=operations_pb2.ExpW(
        target=operations_pb2.Qubit(row=2, col=3),
        axis_half_turns=operations_pb2.ParameterizedFloat(raw=1),
        half_turns=operations_pb2.ParameterizedFloat(parameter_key='k'),
    ))
    assert_proto_dict_convert(gate, proto, cirq.GridQubit(2, 3))

    gate = cirq.PhasedXPowGate(exponent=0.5, phase_exponent=sympy.Symbol('j'))
    proto = operations_pb2.Operation(exp_w=operations_pb2.ExpW(
        target=operations_pb2.Qubit(row=2, col=3),
        axis_half_turns=operations_pb2.ParameterizedFloat(parameter_key='j'),
        half_turns=operations_pb2.ParameterizedFloat(raw=0.5),
    ))
    assert_proto_dict_convert(gate, proto, cirq.GridQubit(2, 3))

    gate = cirq.X**0.25
    proto = operations_pb2.Operation(exp_w=operations_pb2.ExpW(
        target=operations_pb2.Qubit(row=2, col=3),
        axis_half_turns=operations_pb2.ParameterizedFloat(raw=0.0),
        half_turns=operations_pb2.ParameterizedFloat(raw=0.25),
    ))
    assert_proto_dict_convert(gate, proto, cirq.GridQubit(2, 3))

    gate = cirq.Y**0.25
    proto = operations_pb2.Operation(exp_w=operations_pb2.ExpW(
        target=operations_pb2.Qubit(row=2, col=3),
        axis_half_turns=operations_pb2.ParameterizedFloat(raw=0.5),
        half_turns=operations_pb2.ParameterizedFloat(raw=0.25),
    ))
    assert_proto_dict_convert(gate, proto, cirq.GridQubit(2, 3))

    gate = cirq.PhasedXPowGate(exponent=0.5, phase_exponent=sympy.Symbol('j'))
    proto = operations_pb2.Operation(exp_w=operations_pb2.ExpW(
        target=operations_pb2.Qubit(row=2, col=3),
        axis_half_turns=operations_pb2.ParameterizedFloat(parameter_key='j'),
        half_turns=operations_pb2.ParameterizedFloat(raw=0.5),
    ))
    assert_proto_dict_convert(gate, proto, cirq.GridQubit(2, 3))
コード例 #4
0
def gate_to_proto(gate: 'cirq.Gate', qubits: Tuple['cirq.Qid', ...],
                  delay: int) -> operations_pb2.Operation:
    if isinstance(gate, ops.MeasurementGate):
        return operations_pb2.Operation(incremental_delay_picoseconds=delay,
                                        measurement=_measure_to_proto(
                                            gate, qubits))

    if isinstance(gate, ops.XPowGate):
        if len(qubits) != 1:
            # coverage: ignore
            raise ValueError('Wrong number of qubits.')
        return operations_pb2.Operation(incremental_delay_picoseconds=delay,
                                        exp_w=_x_to_proto(gate, qubits[0]))

    if isinstance(gate, ops.YPowGate):
        if len(qubits) != 1:
            # coverage: ignore
            raise ValueError('Wrong number of qubits.')
        return operations_pb2.Operation(incremental_delay_picoseconds=delay,
                                        exp_w=_y_to_proto(gate, qubits[0]))

    if isinstance(gate, ops.PhasedXPowGate):
        if len(qubits) != 1:
            # coverage: ignore
            raise ValueError('Wrong number of qubits.')
        return operations_pb2.Operation(incremental_delay_picoseconds=delay,
                                        exp_w=_phased_x_to_proto(
                                            gate, qubits[0]))

    if isinstance(gate, ops.ZPowGate):
        if len(qubits) != 1:
            # coverage: ignore
            raise ValueError('Wrong number of qubits.')
        return operations_pb2.Operation(incremental_delay_picoseconds=delay,
                                        exp_z=_z_to_proto(gate, qubits[0]))

    if isinstance(gate, ops.CZPowGate):
        if len(qubits) != 2:
            # coverage: ignore
            raise ValueError('Wrong number of qubits.')
        return operations_pb2.Operation(incremental_delay_picoseconds=delay,
                                        exp_11=_cz_to_proto(gate, *qubits))

    raise ValueError(f"Don't know how to serialize this gate: {gate!r}")
コード例 #5
0
def test_single_qubit_measurement_proto_convert():
    gate = cirq.MeasurementGate(1, 'test')
    proto = operations_pb2.Operation(measurement=operations_pb2.Measurement(
        targets=[operations_pb2.Qubit(row=2, col=3)], key='test'))
    assert_proto_dict_convert(gate, proto, cirq.GridQubit(2, 3))