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))
def test_z_proto_convert(): gate = cirq.Z**sympy.Symbol('k') proto = operations_pb2.Operation( exp_z=operations_pb2.ExpZ(target=operations_pb2.Qubit(row=2, col=3), half_turns=operations_pb2.ParameterizedFloat( parameter_key='k'))) assert_proto_dict_convert(gate, proto, cirq.GridQubit(2, 3)) gate = cirq.Z**0.5 proto = operations_pb2.Operation( exp_z=operations_pb2.ExpZ(target=operations_pb2.Qubit(row=2, col=3), half_turns=operations_pb2.ParameterizedFloat( raw=0.5))) assert_proto_dict_convert(gate, proto, cirq.GridQubit(2, 3))
def test_single_qubit_measurement_to_proto_convert_invert_mask(): gate = cirq.MeasurementGate(1, 'test', invert_mask=(True, )) proto = operations_pb2.Operation(measurement=operations_pb2.Measurement( targets=[operations_pb2.Qubit(row=2, col=3)], key='test', invert_mask=[True])) assert_proto_dict_convert(gate, proto, cirq.GridQubit(2, 3))
def test_unsupported_op(): with pytest.raises(ValueError, match='invalid operation'): programs.xmon_op_from_proto(operations_pb2.Operation()) with pytest.raises(ValueError, match='know how to serialize'): programs.gate_to_proto( cirq.CCZ, (cirq.GridQubit(0, 0), cirq.GridQubit(0, 1), cirq.GridQubit(0, 2)), delay=0)
def test_cz_proto_convert(): gate = cirq.CZ**sympy.Symbol('k') proto = operations_pb2.Operation(exp_11=operations_pb2.Exp11( target1=operations_pb2.Qubit(row=2, col=3), target2=operations_pb2.Qubit(row=3, col=4), half_turns=operations_pb2.ParameterizedFloat(parameter_key='k'), )) assert_proto_dict_convert(gate, proto, cirq.GridQubit(2, 3), cirq.GridQubit(3, 4)) gate = cirq.CZ**0.5 proto = operations_pb2.Operation(exp_11=operations_pb2.Exp11( target1=operations_pb2.Qubit(row=2, col=3), target2=operations_pb2.Qubit(row=3, col=4), half_turns=operations_pb2.ParameterizedFloat(raw=0.5), )) assert_proto_dict_convert(gate, proto, cirq.GridQubit(2, 3), cirq.GridQubit(3, 4))
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))
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
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( "Don't know how to serialize this gate: {!r}".format(gate))