Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test gate being added multiple times. #2560

Merged
merged 5 commits into from
Nov 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions cirq/google/devices/known_devices_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,3 +458,54 @@ def test_proto_with_waitgate():
}
}
"""


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.WaitGate(duration=cirq.Duration(nanos=25))(q0)
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"
}
}
"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: add trailing newline

62 changes: 61 additions & 1 deletion cirq/google/serializable_gate_set_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@

Y_DESERIALIZER = cg.GateOpDeserializer(
serialized_gate_id='y_pow',
gate_constructor=cirq.XPowGate,
gate_constructor=cirq.YPowGate,
args=[
cg.DeserializingArg(
serialized_name='half_turns',
Expand Down Expand Up @@ -388,6 +388,66 @@ def test_gateset_with_added_gates():
assert xy_gateset.is_supported_gate(cirq.X)
assert xy_gateset.is_supported_gate(cirq.Y)

# test serialization and deserialization
proto = {
'gate': {
'id': 'y_pow'
},
'args': {
'half_turns': {
'arg_value': {
'float_value': 0.125
}
},
},
'qubits': [{
'id': '1_1'
}]
}

expected_gate = cirq.YPowGate(exponent=0.125)(cirq.GridQubit(1, 1))
assert xy_gateset.serialize_op_dict(expected_gate) == proto
assert xy_gateset.deserialize_op_dict(proto) == expected_gate


def test_gateset_with_added_gates_again():
"""Verify that adding a serializer twice doesn't mess anything up."""
x_gateset = cg.SerializableGateSet(
gate_set_name='x',
serializers=[X_SERIALIZER],
deserializers=[X_DESERIALIZER],
)
xx_gateset = x_gateset.with_added_gates(
gate_set_name='xx',
serializers=[X_SERIALIZER],
deserializers=[X_DESERIALIZER],
)

assert xx_gateset.gate_set_name == 'xx'
assert xx_gateset.is_supported_gate(cirq.X)
assert not xx_gateset.is_supported_gate(cirq.Y)

# test serialization and deserialization
proto = {
'gate': {
'id': 'x_pow'
},
'args': {
'half_turns': {
'arg_value': {
'float_value': 0.125
}
},
},
'qubits': [{
'id': '1_1'
}]
}

expected_gate = cirq.XPowGate(exponent=0.125)(cirq.GridQubit(1, 1))
assert xx_gateset.serialize_op_dict(expected_gate) == proto
assert xx_gateset.deserialize_op_dict(proto) == expected_gate


def test_deserialize_op_invalid_gate():
proto = {
Expand Down