1# Copyright 2020 The Cirq Developers
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14import datetime
15
16from google.protobuf.timestamp_pb2 import Timestamp
17import cirq
18import cirq_google as cg
19
20from cirq_google.engine.client.quantum import types as qtypes
21from cirq_google.engine.client.quantum import enums
22
23
24def test_timeslot_equality():
25    start = datetime.datetime.fromtimestamp(1582592400)
26    end = datetime.datetime.fromtimestamp(1582596000)
27    eq = cirq.testing.equals_tester.EqualsTester()
28    eq.add_equality_group(
29        cg.EngineTimeSlot(processor_id='raining', start_time=start, end_time=end),
30        cg.EngineTimeSlot(processor_id='raining', start_time=start, end_time=end),
31        cg.EngineTimeSlot(
32            processor_id='raining',
33            start_time=start,
34            end_time=end,
35            slot_type=enums.QuantumTimeSlot.TimeSlotType.TIME_SLOT_TYPE_UNSPECIFIED,
36        ),
37    )
38    eq.add_equality_group(
39        cg.EngineTimeSlot(
40            processor_id='raining', start_time=start, end_time=end, project_id='123456'
41        )
42    )
43    eq.add_equality_group(
44        cg.EngineTimeSlot(
45            processor_id='raining',
46            start_time=start,
47            end_time=end,
48            slot_type=enums.QuantumTimeSlot.TimeSlotType.RESERVATION,
49            project_id='123456',
50        )
51    )
52    eq.add_equality_group(
53        cg.EngineTimeSlot(
54            processor_id='raining',
55            start_time=start,
56            end_time=end,
57            slot_type=enums.QuantumTimeSlot.TimeSlotType.MAINTENANCE,
58            project_id='123456',
59        )
60    )
61    eq.add_equality_group(
62        cg.EngineTimeSlot(
63            processor_id='raining',
64            start_time=start,
65            end_time=end,
66            slot_type=enums.QuantumTimeSlot.TimeSlotType.MAINTENANCE,
67            project_id='123456',
68            maintenance_title='Testing',
69            maintenance_description='Testing some new configuration.',
70        )
71    )
72
73
74def test_from_to_proto_plain():
75    slot = enums.QuantumTimeSlot.TimeSlotType.RESERVATION
76    proto = qtypes.QuantumTimeSlot(
77        processor_name='potofgold',
78        start_time=Timestamp(seconds=1500000000),
79        end_time=Timestamp(seconds=1500010000),
80        slot_type=slot,
81    )
82    time_slot = cg.EngineTimeSlot(
83        processor_id='potofgold',
84        start_time=datetime.datetime.fromtimestamp(1500000000),
85        end_time=datetime.datetime.fromtimestamp(1500010000),
86        slot_type=slot,
87    )
88    actual_from_proto = cg.EngineTimeSlot.from_proto(proto)
89    assert actual_from_proto == time_slot
90    actual_to_proto = cg.EngineTimeSlot.to_proto(time_slot)
91    assert actual_to_proto == proto
92
93
94def test_from_to_proto_reservation():
95    slot = enums.QuantumTimeSlot.TimeSlotType.RESERVATION
96    proto = qtypes.QuantumTimeSlot(
97        processor_name='potofgold',
98        start_time=Timestamp(seconds=1500000000),
99        end_time=Timestamp(seconds=1500010000),
100        slot_type=slot,
101        reservation_config=qtypes.QuantumTimeSlot.ReservationConfig(
102            project_id='super_secret_quantum'
103        ),
104    )
105    time_slot = cg.EngineTimeSlot(
106        processor_id='potofgold',
107        start_time=datetime.datetime.fromtimestamp(1500000000),
108        end_time=datetime.datetime.fromtimestamp(1500010000),
109        slot_type=slot,
110        project_id='super_secret_quantum',
111    )
112    actual_from_proto = cg.EngineTimeSlot.from_proto(proto)
113    assert actual_from_proto == time_slot
114    actual_to_proto = cg.EngineTimeSlot.to_proto(time_slot)
115    assert actual_to_proto == proto
116
117
118def test_from_to_proto_maintenance():
119    slot = enums.QuantumTimeSlot.TimeSlotType.MAINTENANCE
120    proto = qtypes.QuantumTimeSlot(
121        processor_name='potofgold',
122        start_time=Timestamp(seconds=1500020000),
123        end_time=Timestamp(seconds=1500040000),
124        slot_type=slot,
125        maintenance_config=qtypes.QuantumTimeSlot.MaintenanceConfig(
126            title='Testing',
127            description='Testing some new configuration.',
128        ),
129    )
130    time_slot = cg.EngineTimeSlot(
131        processor_id='potofgold',
132        start_time=datetime.datetime.fromtimestamp(1500020000),
133        end_time=datetime.datetime.fromtimestamp(1500040000),
134        slot_type=slot,
135        maintenance_title='Testing',
136        maintenance_description='Testing some new configuration.',
137    )
138    actual_from_proto = cg.EngineTimeSlot.from_proto(proto)
139    assert actual_from_proto == time_slot
140    actual_to_proto = cg.EngineTimeSlot.to_proto(time_slot)
141    assert actual_to_proto == proto
142
143
144def test_from_to_proto_no_end_time():
145    slot = enums.QuantumTimeSlot.TimeSlotType.MAINTENANCE
146    proto = qtypes.QuantumTimeSlot(
147        processor_name='potofgold',
148        end_time=Timestamp(seconds=1500040000),
149        slot_type=slot,
150        maintenance_config=qtypes.QuantumTimeSlot.MaintenanceConfig(
151            title='Testing',
152            description='Testing some new configuration.',
153        ),
154    )
155    time_slot = cg.EngineTimeSlot(
156        processor_id='potofgold',
157        end_time=datetime.datetime.fromtimestamp(1500040000),
158        slot_type=slot,
159        maintenance_title='Testing',
160        maintenance_description='Testing some new configuration.',
161    )
162    actual_from_proto = cg.EngineTimeSlot.from_proto(proto)
163    assert actual_from_proto == time_slot
164    actual_to_proto = cg.EngineTimeSlot.to_proto(time_slot)
165    assert actual_to_proto == proto
166
167
168def test_from_to_proto_no_start_time():
169    slot = enums.QuantumTimeSlot.TimeSlotType.MAINTENANCE
170    proto = qtypes.QuantumTimeSlot(
171        processor_name='potofgold',
172        start_time=Timestamp(seconds=1500040000),
173        slot_type=slot,
174        maintenance_config=qtypes.QuantumTimeSlot.MaintenanceConfig(
175            title='Testing',
176            description='Testing some new configuration.',
177        ),
178    )
179    time_slot = cg.EngineTimeSlot(
180        processor_id='potofgold',
181        start_time=datetime.datetime.fromtimestamp(1500040000),
182        slot_type=slot,
183        maintenance_title='Testing',
184        maintenance_description='Testing some new configuration.',
185    )
186    actual_from_proto = cg.EngineTimeSlot.from_proto(proto)
187    assert actual_from_proto == time_slot
188    actual_to_proto = cg.EngineTimeSlot.to_proto(time_slot)
189    assert actual_to_proto == proto
190