def test_single_qubit_measurement_invalid_dict(): proto_dict = { 'measurement': { 'targets': [ { 'row': 2, 'col': 3 } ], } } with pytest.raises(ValueError): cg.xmon_op_from_proto_dict(proto_dict) proto_dict = { 'measurement': { 'targets': [ { 'row': 2, 'col': 3 } ], } } with pytest.raises(ValueError): cg.xmon_op_from_proto_dict(proto_dict)
def test_z_invalid_dict(): proto_dict = { 'exp_z': { 'target': { 'row': 2, 'col': 3 }, } } with pytest.raises(ValueError): cg.xmon_op_from_proto_dict(proto_dict) proto_dict = {'exp_z': {'half_turns': {'parameter_key': 'k'}}} with pytest.raises(ValueError): cg.xmon_op_from_proto_dict(proto_dict)
def test_unsupported_op(): proto_dict = { 'not_a_gate': { 'target': { 'row': 2, 'col': 3 }, } } with pytest.raises(ValueError, match='invalid operation'): cg.xmon_op_from_proto_dict(proto_dict) with pytest.raises(ValueError, match='know how to serialize'): cg.gate_to_proto_dict( cirq.CCZ, (cirq.GridQubit(0, 0), cirq.GridQubit(0, 1), cirq.GridQubit(0, 2)))
def test_invalid_qubit_from_proto(): with pytest.raises(ValueError, match='does not contain row or col'): proto_dict = { 'exp_z': { 'target': { 'row': 2, }, 'half_turns': { 'raw': 1 }, } } _ = cg.xmon_op_from_proto_dict(proto_dict)
def test_proto_dict_convert_invert_mask_bools(): gate = cirq.MeasurementGate(1, 'test', invert_mask=(True,)) proto_dict = { 'measurement': { 'targets': [{ 'row': 2, 'col': 3 }], 'key': 'test', 'invert_mask': [True] } } # Conversion only works one way, since the reverse converts True to string assert cg.xmon_op_from_proto_dict(proto_dict) == gate(cirq.GridQubit(2, 3))
def test_cz_invalid_dict(): proto_dict = { 'exp_11': { 'target2': { 'row': 3, 'col': 4 }, 'half_turns': { 'parameter_key': 'k' } } } with pytest.raises(ValueError, match='missing required fields'): cg.xmon_op_from_proto_dict(proto_dict) proto_dict = { 'exp_11': { 'target1': { 'row': 2, 'col': 3 }, 'half_turns': { 'parameter_key': 'k' } } } with pytest.raises(ValueError, match='missing required fields'): cg.xmon_op_from_proto_dict(proto_dict) proto_dict = { 'exp_11': { 'target1': { 'row': 2, 'col': 3 }, 'target2': { 'row': 3, 'col': 4 }, } } with pytest.raises(ValueError, match='missing required fields'): cg.xmon_op_from_proto_dict(proto_dict)
def assert_proto_dict_convert(gate: cirq.Gate, proto_dict: Dict, *qubits: cirq.QubitId): assert cg.gate_to_proto_dict(gate, qubits) == proto_dict assert cg.xmon_op_from_proto_dict(proto_dict) == gate(*qubits)