-
Notifications
You must be signed in to change notification settings - Fork 651
Expand file tree
/
Copy pathserializer.py
More file actions
1050 lines (884 loc) · 40.9 KB
/
serializer.py
File metadata and controls
1050 lines (884 loc) · 40.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2020 The TensorFlow Quantum Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""A basic serializer used to serialize/deserialize Cirq circuits for tfq."""
# TODO(pmassey / anyone): determine if this should be kept as globals.
import copy
import numbers
import sympy
import numpy as np
import cirq
from tensorflow_quantum.core.serialize import op_serializer, op_deserializer, \
serializable_gate_set
from tensorflow_quantum.core.proto import pauli_sum_pb2
from tensorflow_quantum.core.proto import program_pb2
from tensorflow_quantum.core.proto import projector_sum_pb2
# Needed to allow autograph to crawl AST without erroring.
_CONSTANT_TRUE = lambda x: True
def _single_qubit_channel_check(x):
"""Check that a noise channel operates on exactly one qubit."""
if len(x.qubits) != 1:
raise ValueError(
"Multi-qubit noise channels are not supported in TFQ. "
f"Got {x} acting on {len(x.qubits)} qubits. "
"Consider decomposing into single-qubit channels applied "
"via .on_each().")
return True
def _round(x):
return np.round(x, 6) if isinstance(x, float) else x
def _parse_mul(expr):
"""Returns the lhs and rhs of a sympy.Mul. This is written
to prevent autograph from going into sympy library code and having
conflicts with the @cacheit decorator."""
if len(expr.args) == 1:
return sympy.S.One, expr.args[0]
if len(expr.args) == 2:
return expr.args[0], expr.args[1]
raise ValueError("Arithmetic expression outside of simple "
"scalar multiplication is currently not "
"supported. See serializer.py for more "
"information.")
def _scalar_extractor(x):
"""This is a workaround to support symbol scalar multiplication.
In the future we should likely get rid of this in favor of proper
expression parsing once cirq supports it. See cirq.op_serializer
and cirq's program protobuf for details. This is needed for things
like cirq.rx('alpha').
"""
if not isinstance(x, (numbers.Real, sympy.Expr)):
raise TypeError("Invalid input argument for exponent.")
if isinstance(x, (numbers.Real, sympy.Symbol)):
return 1.0
expr = x.evalf()
if isinstance(expr, sympy.core.Mul):
lhs_eval, rhs_eval = _parse_mul(expr)
if isinstance(lhs_eval, sympy.Symbol) and isinstance(
rhs_eval,
(sympy.core.numbers.Float, sympy.core.numbers.Integer)):
# lhs contains symbol rhs contains number.
return _round(float(rhs_eval))
if isinstance(rhs_eval, sympy.Symbol) and isinstance(
lhs_eval,
(sympy.core.numbers.Float, sympy.core.numbers.Integer)):
# lhs contains number.
return _round(float(lhs_eval))
raise ValueError("Arithmetic expression outside of simple "
"scalar multiplication is currently not "
"supported. See serializer.py for more "
"information.")
def _symbol_extractor(x):
"""This is the second extractor for above."""
if not isinstance(x, (numbers.Real, sympy.Expr)):
raise TypeError("Invalid input argument for exponent.")
if isinstance(x, numbers.Real):
return _round(float(x))
if isinstance(x, sympy.Symbol):
return x
expr = x.evalf()
if isinstance(expr, sympy.core.Mul):
lhs_eval, rhs_eval = _parse_mul(expr)
if isinstance(lhs_eval, sympy.Symbol) and isinstance(
rhs_eval,
(sympy.core.numbers.Float, sympy.core.numbers.Integer)):
# lhs contains symbol rhs contains number.
return lhs_eval
if isinstance(rhs_eval, sympy.Symbol) and isinstance(
lhs_eval,
(sympy.core.numbers.Float, sympy.core.numbers.Integer)):
# lhs contains number.
return rhs_eval
raise ValueError("Arithmetic expression outside of simple "
"scalar multiplication is currently not "
"supported. See serializer.py for more "
"information.")
def _serialize_controls(gate):
"""Helper to serialize control qubits if applicable."""
if hasattr(gate, '_tfq_control_qubits'):
return ','.join(
op_serializer.qubit_to_proto(q) for q in gate._tfq_control_qubits)
return ''
def _serialize_control_vals(gate):
"""Helper to serialize control values if applicable.."""
if hasattr(gate, '_tfq_control_values'):
return ','.join(str(v[0]) for v in gate._tfq_control_values)
return ''
class DelayedAssignmentGate(cirq.Gate):
"""Class to do control qubit assignment before sub_gate qubit assignment."""
def __init__(self, gate_callable, control_qubits, control_values):
self._gate_callable = gate_callable
self._control_qubits = control_qubits
self._control_values = control_values
def _qid_shape_(self):
raise ValueError("Called qid_shape on workaround class.")
# pylint: disable=invalid-name
def on(self, *qubits):
"""Returns gate_callable on qubits controlled by contol_qubits."""
gate = self._gate_callable(*qubits)
# TODO(tonybruguier,#636): Here we call the parent's class controlled_by
# because Cirq's breaking change #4167 created 3-qubit gates that cannot
# be serialized yet. Instead, support 3-qubit gates and revert the
# work-around.
if len(self._control_qubits) == 0:
return gate
return cirq.ControlledOperation(self._control_qubits,
gate,
control_values=self._control_values)
# pylint: enable=invalid-name
def _optional_control_promote(gate, qubits_message, values_message):
"""Optionally promote to controlled gate based on serialized control msg."""
if qubits_message == '' and values_message == '':
return gate
qbs = [
op_deserializer.qubit_from_proto(qb) for qb in qubits_message.split(',')
]
vals = [int(cv) for cv in values_message.split(',')]
return DelayedAssignmentGate(gate, qbs, vals)
# Channels.
def _asymmetric_depolarize_serializer():
"""Make standard serializer for asymmetric depolarization channel."""
args = [
# cirq channels can't contain symbols.
op_serializer.SerializingArg(serialized_name="p_x",
serialized_type=float,
op_getter=lambda x: x.gate.p_x),
op_serializer.SerializingArg(serialized_name="p_y",
serialized_type=float,
op_getter=lambda x: x.gate.p_y),
op_serializer.SerializingArg(serialized_name="p_z",
serialized_type=float,
op_getter=lambda x: x.gate.p_z),
op_serializer.SerializingArg(serialized_name="control_qubits",
serialized_type=str,
op_getter=lambda x: ''),
op_serializer.SerializingArg(serialized_name="control_values",
serialized_type=str,
op_getter=lambda x: '')
]
return op_serializer.GateOpSerializer(
gate_type=cirq.AsymmetricDepolarizingChannel,
serialized_gate_id="ADP",
args=args,
can_serialize_predicate=_single_qubit_channel_check)
def _asymmetric_depolarize_deserializer():
"""Make standard deserializer for asymmetric depolarization channel."""
args = [
op_deserializer.DeserializingArg(serialized_name="p_x",
constructor_arg_name="p_x"),
op_deserializer.DeserializingArg(serialized_name="p_y",
constructor_arg_name="p_y"),
op_deserializer.DeserializingArg(serialized_name="p_z",
constructor_arg_name="p_z")
]
return op_deserializer.GateOpDeserializer(
serialized_gate_id="ADP",
gate_constructor=cirq.AsymmetricDepolarizingChannel,
args=args)
def _depolarize_channel_serializer():
"""Make standard serializer for depolarization channel."""
args = [
# cirq channels can't contain symbols.
op_serializer.SerializingArg(serialized_name="p",
serialized_type=float,
op_getter=lambda x: x.gate.p),
op_serializer.SerializingArg(serialized_name="control_qubits",
serialized_type=str,
op_getter=lambda x: ''),
op_serializer.SerializingArg(serialized_name="control_values",
serialized_type=str,
op_getter=lambda x: '')
]
return op_serializer.GateOpSerializer(
gate_type=cirq.DepolarizingChannel,
serialized_gate_id="DP",
args=args,
can_serialize_predicate=_single_qubit_channel_check)
def _depolarize_channel_deserializer():
"""Make standard deserializer for depolarization channel."""
args = [
op_deserializer.DeserializingArg(serialized_name="p",
constructor_arg_name="p")
]
return op_deserializer.GateOpDeserializer(
serialized_gate_id="DP",
gate_constructor=cirq.DepolarizingChannel,
args=args)
def _gad_channel_serializer():
"""Make standard serializer for GeneralizedAmplitudeDamping."""
args = [
# cirq channels can't contain symbols.
op_serializer.SerializingArg(serialized_name="p",
serialized_type=float,
op_getter=lambda x: x.gate.p),
op_serializer.SerializingArg(serialized_name="gamma",
serialized_type=float,
op_getter=lambda x: x.gate.gamma),
op_serializer.SerializingArg(serialized_name="control_qubits",
serialized_type=str,
op_getter=lambda x: ''),
op_serializer.SerializingArg(serialized_name="control_values",
serialized_type=str,
op_getter=lambda x: '')
]
return op_serializer.GateOpSerializer(
gate_type=cirq.GeneralizedAmplitudeDampingChannel,
serialized_gate_id="GAD",
args=args,
can_serialize_predicate=_single_qubit_channel_check)
def _gad_channel_deserializer():
"""Make standard deserializer for GeneralizedAmplitudeDamping."""
args = [
op_deserializer.DeserializingArg(serialized_name="p",
constructor_arg_name="p"),
op_deserializer.DeserializingArg(serialized_name="gamma",
constructor_arg_name="gamma")
]
return op_deserializer.GateOpDeserializer(
serialized_gate_id="GAD",
gate_constructor=cirq.GeneralizedAmplitudeDampingChannel,
args=args)
def _amplitude_damp_channel_serializer():
"""Make standard serializer for AmplitudeDamp channel."""
args = [
# cirq channels can't contain symbols.
op_serializer.SerializingArg(serialized_name="gamma",
serialized_type=float,
op_getter=lambda x: x.gate.gamma),
op_serializer.SerializingArg(serialized_name="control_qubits",
serialized_type=str,
op_getter=lambda x: ''),
op_serializer.SerializingArg(serialized_name="control_values",
serialized_type=str,
op_getter=lambda x: '')
]
return op_serializer.GateOpSerializer(
gate_type=cirq.AmplitudeDampingChannel,
serialized_gate_id="AD",
args=args,
can_serialize_predicate=_single_qubit_channel_check)
def _amplitude_damp_channel_deserializer():
"""Make standard deserializer for depolarization channel."""
args = [
op_deserializer.DeserializingArg(serialized_name="gamma",
constructor_arg_name="gamma")
]
return op_deserializer.GateOpDeserializer(
serialized_gate_id="AD",
gate_constructor=cirq.AmplitudeDampingChannel,
args=args)
def _reset_channel_serializer():
"""Make standard serializer for reset channel."""
args = [
# cirq channels can't contain symbols.
op_serializer.SerializingArg(serialized_name="control_qubits",
serialized_type=str,
op_getter=lambda x: ''),
op_serializer.SerializingArg(serialized_name="control_values",
serialized_type=str,
op_getter=lambda x: '')
]
return op_serializer.GateOpSerializer(
gate_type=cirq.ResetChannel,
serialized_gate_id="RST",
args=args,
can_serialize_predicate=_single_qubit_channel_check)
def _reset_channel_deserializer():
"""Make standard deserializer for reset channel."""
args = []
return op_deserializer.GateOpDeserializer(
serialized_gate_id="RST", gate_constructor=cirq.ResetChannel, args=args)
def _phase_damp_channel_serializer():
"""Make standard serializer for PhaseDamp channel."""
args = [
# cirq channels can't contain symbols.
op_serializer.SerializingArg(serialized_name="gamma",
serialized_type=float,
op_getter=lambda x: x.gate.gamma),
op_serializer.SerializingArg(serialized_name="control_qubits",
serialized_type=str,
op_getter=lambda x: ''),
op_serializer.SerializingArg(serialized_name="control_values",
serialized_type=str,
op_getter=lambda x: '')
]
return op_serializer.GateOpSerializer(
gate_type=cirq.PhaseDampingChannel,
serialized_gate_id="PD",
args=args,
can_serialize_predicate=_single_qubit_channel_check)
def _phase_damp_channel_deserializer():
"""Make standard deserializer for PhaseDamp channel."""
args = [
op_deserializer.DeserializingArg(serialized_name="gamma",
constructor_arg_name="gamma")
]
return op_deserializer.GateOpDeserializer(
serialized_gate_id="PD",
gate_constructor=cirq.PhaseDampingChannel,
args=args)
def _phase_flip_channel_serializer():
"""Make standard serializer for PhaseFlip channel."""
args = [
# cirq channels can't contain symbols.
op_serializer.SerializingArg(serialized_name="p",
serialized_type=float,
op_getter=lambda x: x.gate.p),
op_serializer.SerializingArg(serialized_name="control_qubits",
serialized_type=str,
op_getter=lambda x: ''),
op_serializer.SerializingArg(serialized_name="control_values",
serialized_type=str,
op_getter=lambda x: '')
]
return op_serializer.GateOpSerializer(
gate_type=cirq.PhaseFlipChannel,
serialized_gate_id="PF",
args=args,
can_serialize_predicate=_single_qubit_channel_check)
def _phase_flip_channel_deserializer():
"""Make standard deserializer for PhaseFlip channel."""
args = [
op_deserializer.DeserializingArg(serialized_name="p",
constructor_arg_name="p")
]
return op_deserializer.GateOpDeserializer(
serialized_gate_id="PF",
gate_constructor=cirq.PhaseFlipChannel,
args=args)
def _bit_flip_channel_serializer():
"""Make standard serializer for BitFlip channel."""
args = [
# cirq channels can't contain symbols.
op_serializer.SerializingArg(serialized_name="p",
serialized_type=float,
op_getter=lambda x: x.gate.p),
op_serializer.SerializingArg(serialized_name="control_qubits",
serialized_type=str,
op_getter=lambda x: ''),
op_serializer.SerializingArg(serialized_name="control_values",
serialized_type=str,
op_getter=lambda x: '')
]
return op_serializer.GateOpSerializer(
gate_type=cirq.BitFlipChannel,
serialized_gate_id="BF",
args=args,
can_serialize_predicate=_single_qubit_channel_check)
def _bit_flip_channel_deserializer():
"""Make standard deserializer for BitFlip channel."""
args = [
op_deserializer.DeserializingArg(serialized_name="p",
constructor_arg_name="p")
]
return op_deserializer.GateOpDeserializer(
serialized_gate_id="BF",
gate_constructor=cirq.BitFlipChannel,
args=args)
# Gates.
def _eigen_gate_serializer(gate_type, serialized_id):
"""Make standard serializer for eigen gates."""
args = [
op_serializer.SerializingArg(
serialized_name="exponent",
serialized_type=float,
op_getter=lambda x: _symbol_extractor(x.gate.exponent)),
op_serializer.SerializingArg(
serialized_name="exponent_scalar",
serialized_type=float,
op_getter=lambda x: _scalar_extractor(x.gate.exponent)),
op_serializer.SerializingArg(
serialized_name="global_shift",
serialized_type=float,
op_getter=lambda x: float(x.gate._global_shift)),
op_serializer.SerializingArg(
serialized_name="control_qubits",
serialized_type=str,
op_getter=lambda x: _serialize_controls(x)),
op_serializer.SerializingArg(
serialized_name="control_values",
serialized_type=str,
op_getter=lambda x: _serialize_control_vals(x))
]
return op_serializer.GateOpSerializer(
gate_type=gate_type,
serialized_gate_id=serialized_id,
args=args,
can_serialize_predicate=_CONSTANT_TRUE)
def _eigen_gate_deserializer(gate_type, serialized_id):
"""Make standard deserializer for eigen gates."""
def _scalar_combiner(exponent, global_shift, exponent_scalar,
control_qubits, control_values):
"""This is a workaround to support symbol scalar multiplication.
In the future we should likely get rid of this in favor of proper
expression parsing once cirq supports it. See cirq.op_serializer
and cirq's program protobuf for details. This is needed for things
like cirq.rx('alpha').
"""
if exponent_scalar == 1.0:
return _optional_control_promote(
gate_type(exponent=_round(exponent),
global_shift=_round(global_shift)), control_qubits,
control_values)
return _optional_control_promote(
gate_type(exponent=_round(exponent) * _round(exponent_scalar),
global_shift=_round(global_shift)), control_qubits,
control_values)
args = [
op_deserializer.DeserializingArg(serialized_name="exponent",
constructor_arg_name="exponent"),
op_deserializer.DeserializingArg(serialized_name="global_shift",
constructor_arg_name="global_shift"),
op_deserializer.DeserializingArg(
serialized_name="exponent_scalar",
constructor_arg_name="exponent_scalar"),
op_deserializer.DeserializingArg(serialized_name="control_qubits",
constructor_arg_name="control_qubits"),
op_deserializer.DeserializingArg(serialized_name="control_values",
constructor_arg_name="control_values")
]
return op_deserializer.GateOpDeserializer(serialized_gate_id=serialized_id,
gate_constructor=_scalar_combiner,
args=args)
def _fsim_gate_serializer():
"""Make standard serializer for fsim gate."""
args = [
op_serializer.SerializingArg(
serialized_name="theta",
serialized_type=float,
op_getter=lambda x: _symbol_extractor(x.gate.theta)),
op_serializer.SerializingArg(
serialized_name="phi",
serialized_type=float,
op_getter=lambda x: _symbol_extractor(x.gate.phi)),
op_serializer.SerializingArg(
serialized_name="theta_scalar",
serialized_type=float,
op_getter=lambda x: _scalar_extractor(x.gate.theta)),
op_serializer.SerializingArg(
serialized_name="phi_scalar",
serialized_type=float,
op_getter=lambda x: _scalar_extractor(x.gate.phi)),
op_serializer.SerializingArg(
serialized_name="control_qubits",
serialized_type=str,
op_getter=lambda x: _serialize_controls(x)),
op_serializer.SerializingArg(
serialized_name="control_values",
serialized_type=str,
op_getter=lambda x: _serialize_control_vals(x))
]
return op_serializer.GateOpSerializer(
gate_type=cirq.FSimGate,
serialized_gate_id="FSIM",
args=args,
can_serialize_predicate=_CONSTANT_TRUE)
def _fsim_gate_deserializer():
"""Make standard deserializer for fsim gate."""
def _scalar_combiner(theta, theta_scalar, phi, phi_scalar, control_qubits,
control_values):
"""This is a workaround to support symbol scalar multiplication.
See `_eigen_gate_deserializer` for details.
"""
return _optional_control_promote(
cirq.FSimGate(theta=_round(theta) * _round(theta_scalar),
phi=_round(phi) * _round(phi_scalar)), control_qubits,
control_values)
args = [
op_deserializer.DeserializingArg(serialized_name="theta",
constructor_arg_name="theta"),
op_deserializer.DeserializingArg(serialized_name="phi",
constructor_arg_name="phi"),
op_deserializer.DeserializingArg(serialized_name="theta_scalar",
constructor_arg_name="theta_scalar"),
op_deserializer.DeserializingArg(serialized_name="phi_scalar",
constructor_arg_name="phi_scalar"),
op_deserializer.DeserializingArg(serialized_name="control_qubits",
constructor_arg_name="control_qubits"),
op_deserializer.DeserializingArg(serialized_name="control_values",
constructor_arg_name="control_values")
]
return op_deserializer.GateOpDeserializer(serialized_gate_id="FSIM",
gate_constructor=_scalar_combiner,
args=args)
def _identity_gate_serializer():
"""Make a standard serializer for the single qubit identity."""
def _identity_check(x):
if x.gate.num_qubits() != 1:
raise ValueError("Multi-Qubit identity gate not supported."
f"Given: {x}. To work around this, use "
"cirq.I.on_each instead.")
return True
# Here `args` is used for two reasons. 1. GateOpSerializer doesn't work well
# with empty arg lists. 2. It is a nice way to check identity gate size.
args = [
op_serializer.SerializingArg(serialized_name="unused",
serialized_type=bool,
op_getter=_identity_check),
op_serializer.SerializingArg(
serialized_name="control_qubits",
serialized_type=str,
op_getter=lambda x: _serialize_controls(x)),
op_serializer.SerializingArg(
serialized_name="control_values",
serialized_type=str,
op_getter=lambda x: _serialize_control_vals(x))
]
return op_serializer.GateOpSerializer(
gate_type=cirq.IdentityGate,
serialized_gate_id="I",
args=args,
can_serialize_predicate=_CONSTANT_TRUE)
def _identity_gate_deserializer():
"""Make a standard deserializer for the single qubit identity."""
args = [
op_deserializer.DeserializingArg(serialized_name="unused",
constructor_arg_name="unused"),
op_deserializer.DeserializingArg(serialized_name="control_qubits",
constructor_arg_name="control_qubits"),
op_deserializer.DeserializingArg(serialized_name="control_values",
constructor_arg_name="control_values")
]
def _cirq_i_workaround(unused, control_qubits, control_values):
return _optional_control_promote(cirq.I, control_qubits, control_values)
return op_deserializer.GateOpDeserializer(
serialized_gate_id="I", gate_constructor=_cirq_i_workaround, args=args)
def _phased_eigen_gate_serializer(gate_type, serialized_id):
"""Make a standard serializer for phased eigen gates."""
args = [
op_serializer.SerializingArg(
serialized_name="phase_exponent",
serialized_type=float,
op_getter=lambda x: _symbol_extractor(x.gate.phase_exponent)),
op_serializer.SerializingArg(
serialized_name="phase_exponent_scalar",
serialized_type=float,
op_getter=lambda x: _scalar_extractor(x.gate.phase_exponent)),
op_serializer.SerializingArg(
serialized_name="exponent",
serialized_type=float,
op_getter=lambda x: _symbol_extractor(x.gate.exponent)),
op_serializer.SerializingArg(
serialized_name="exponent_scalar",
serialized_type=float,
op_getter=lambda x: _scalar_extractor(x.gate.exponent)),
op_serializer.SerializingArg(
serialized_name="global_shift",
serialized_type=float,
op_getter=lambda x: float(x.gate.global_shift)),
op_serializer.SerializingArg(
serialized_name="control_qubits",
serialized_type=str,
op_getter=lambda x: _serialize_controls(x)),
op_serializer.SerializingArg(
serialized_name="control_values",
serialized_type=str,
op_getter=lambda x: _serialize_control_vals(x))
]
return op_serializer.GateOpSerializer(
gate_type=gate_type,
serialized_gate_id=serialized_id,
args=args,
can_serialize_predicate=_CONSTANT_TRUE)
def _phased_eigen_gate_deserializer(gate_type, serialized_id):
"""Make a standard deserializer for phased eigen gates."""
def _scalar_combiner(exponent, global_shift, exponent_scalar,
phase_exponent, phase_exponent_scalar, control_qubits,
control_values):
"""This is a workaround to support symbol scalar multiplication.
In the future we should likely get rid of this in favor of proper
expression parsing once cirq supports it. See cirq.op_serializer
and cirq's program protobuf for details. This is needed for things
like cirq.rx('alpha').
"""
exponent = _round(exponent)
phase_exponent = _round(phase_exponent)
exponent = exponent if exponent_scalar == 1.0 \
else exponent * _round(exponent_scalar)
phase_exponent = phase_exponent if phase_exponent_scalar == 1.0 \
else phase_exponent * _round(phase_exponent_scalar)
if global_shift != 0:
# needed in case this specific phasedeigengate doesn't
# have a global_phase in constructor.
return _optional_control_promote(
gate_type(exponent=exponent,
global_shift=_round(global_shift),
phase_exponent=phase_exponent), control_qubits,
control_values)
return _optional_control_promote(
gate_type(exponent=exponent, phase_exponent=phase_exponent),
control_qubits, control_values)
args = [
op_deserializer.DeserializingArg(serialized_name="phase_exponent",
constructor_arg_name="phase_exponent"),
op_deserializer.DeserializingArg(
serialized_name="phase_exponent_scalar",
constructor_arg_name="phase_exponent_scalar"),
op_deserializer.DeserializingArg(serialized_name="exponent",
constructor_arg_name="exponent"),
op_deserializer.DeserializingArg(
serialized_name="exponent_scalar",
constructor_arg_name="exponent_scalar"),
op_deserializer.DeserializingArg(serialized_name="global_shift",
constructor_arg_name="global_shift"),
op_deserializer.DeserializingArg(serialized_name="control_qubits",
constructor_arg_name="control_qubits"),
op_deserializer.DeserializingArg(serialized_name="control_values",
constructor_arg_name="control_values")
]
return op_deserializer.GateOpDeserializer(serialized_gate_id=serialized_id,
gate_constructor=_scalar_combiner,
args=args)
EIGEN_GATES_DICT = {
cirq.XPowGate: "XP",
cirq.XXPowGate: "XXP",
cirq.YPowGate: "YP",
cirq.YYPowGate: "YYP",
cirq.ZPowGate: "ZP",
cirq.ZZPowGate: "ZZP",
cirq.HPowGate: "HP",
cirq.CZPowGate: "CZP",
cirq.CNotPowGate: "CNP",
cirq.SwapPowGate: "SP",
cirq.ISwapPowGate: "ISP",
}
PHASED_EIGEN_GATES_DICT = {
cirq.PhasedXPowGate: "PXP",
cirq.PhasedISwapPowGate: "PISP",
}
SERIALIZERS = [
_eigen_gate_serializer(g, g_name) for g, g_name in EIGEN_GATES_DICT.items()
] + [
_phased_eigen_gate_serializer(g, g_name)
for g, g_name in PHASED_EIGEN_GATES_DICT.items()
] + [
_amplitude_damp_channel_serializer(),
_asymmetric_depolarize_serializer(),
_bit_flip_channel_serializer(),
_depolarize_channel_serializer(),
_fsim_gate_serializer(),
_gad_channel_serializer(),
_identity_gate_serializer(),
_phase_damp_channel_serializer(),
_reset_channel_serializer(),
_phase_flip_channel_serializer()
]
DESERIALIZERS = [
_eigen_gate_deserializer(g, g_name)
for g, g_name in EIGEN_GATES_DICT.items()
] + [
_phased_eigen_gate_deserializer(g, g_name)
for g, g_name in PHASED_EIGEN_GATES_DICT.items()
] + [
_amplitude_damp_channel_deserializer(),
_asymmetric_depolarize_deserializer(),
_bit_flip_channel_deserializer(),
_depolarize_channel_deserializer(),
_fsim_gate_deserializer(),
_gad_channel_deserializer(),
_identity_gate_deserializer(),
_phase_damp_channel_deserializer(),
_reset_channel_deserializer(),
_phase_flip_channel_deserializer()
]
SERIALIZER = serializable_gate_set.SerializableGateSet(
gate_set_name="tfq_gate_set",
serializers=SERIALIZERS,
deserializers=DESERIALIZERS)
def serialize_circuit(circuit_inp):
"""Returns a `cirq.Program` proto representing the `cirq.Circuit`.
Note that the circuit must use gates valid in the tfq_gate_set.
Currently we only support scalar multiplication of symbols and
no other more complex arithmetic expressions. This means
we can support things like X**(3*alpha), and Rx(alpha). Because
we use the `cirq.Program` proto, we only support `cirq.GridQubit`
and `cirq.LineQubit` instances during serialization of circuits.
Note: once serialized terminal measurements are removed.
Args:
circuit_inp: A `cirq.Circuit`.
Returns:
A `tfq.proto.Program` proto.
"""
circuit = copy.deepcopy(circuit_inp)
if not isinstance(circuit, cirq.Circuit):
raise TypeError("serialize requires cirq.Circuit objects."
" Given: " + str(type(circuit)))
# This code is intentionally written to avoid using cirq functions
# as this get analyzed by tensorflow-autograph.
# Gives a map from moment index to measure qubits in moment
measured_moments = dict()
# Tracks qubits that have been measured already.
all_measured_qubits = set()
for i, moment in enumerate(circuit.moments):
measured_qubits = set()
for op in moment:
for qubit in op.qubits:
if not isinstance(qubit, (cirq.GridQubit, cirq.LineQubit)):
raise ValueError(
"Attempted to serialize circuit that don't use "
"only cirq.GridQubits or cirq.LineQubits.")
if isinstance(op.gate, cirq.MeasurementGate):
for qubit in op.qubits:
if qubit in all_measured_qubits:
raise ValueError("Serialization of circuit failed. "
"Circuits with non-terminal "
"measurement operations are not "
"supported.")
measured_qubits.add(qubit)
all_measured_qubits.add(qubit)
if len(measured_qubits) > 0:
measured_moments[i] = measured_qubits
# Remove terminal measurements.
for moment_ind in measured_moments:
old_moment = circuit[moment_ind]
measured_qubits = measured_moments[moment_ind]
new_moment = cirq.Moment(
filter(lambda x: not any(y in measured_qubits for y in x.qubits),
old_moment.operations))
circuit[moment_ind] = new_moment
# Demote cirq.controlled_operations (controlled gates) to their sub_gate
# types with _tfq_control_qubits and _tfq_control_values fields so that
# the gates can still get picked up by the serializer. There would be no way
# to discern controlledgates from one another otherwise. This
# "momentary demotion" occurs with the help of the DelayedAssignmentGate.
for i, moment in enumerate(circuit):
controlled_ops = [
op for op in moment if isinstance(op, cirq.ControlledOperation)
]
new_ops = dict()
for op in controlled_ops:
tfq_compatible = op.sub_operation
tfq_compatible._tfq_control_qubits = op.controls
tfq_compatible._tfq_control_values = op.control_values
new_ops[op.qubits] = tfq_compatible
circuit[i] = cirq.Moment(
new_ops[op.qubits] if op.qubits in new_ops else op for op in moment)
return SERIALIZER.serialize(circuit)
def deserialize_circuit(proto):
"""Constructs a `cirq.Circuit` from a `cirq.Program` proto.
Note that the proto must use gates valid in the tfq_gate_set.
Args:
proto: A `tfq.proto.Program` proto
Returns:
A `cirq.Circuit`.
"""
if not isinstance(proto, program_pb2.Program):
raise TypeError("deserialize requires "
"tfq.proto.Program object."
" Given: " + str(type(proto)))
return SERIALIZER.deserialize(proto)
def serialize_paulisum(paulisum):
"""Constructs a pauli_sum proto from `cirq.PauliSum` or `cirq.PauliString`.
Args:
paulisum: A `cirq.PauliSum` object.
Returns:
A pauli_sum proto object.
"""
if isinstance(paulisum, cirq.PauliString):
paulisum = cirq.PauliSum.from_pauli_strings(paulisum)
if not isinstance(paulisum, cirq.PauliSum):
raise TypeError("serialize requires a cirq.PauliSum object."
" Given: " + str(type(paulisum)))
if any(not isinstance(qubit, (cirq.LineQubit, cirq.GridQubit))
for qubit in paulisum.qubits):
raise ValueError("Attempted to serialize a paulisum that doesn't use "
"only cirq.GridQubits or cirq.LineQubits.")
paulisum_proto = pauli_sum_pb2.PauliSum()
for term in paulisum:
pauliterm_proto = pauli_sum_pb2.PauliTerm()
pauliterm_proto.coefficient_real = term.coefficient.real
pauliterm_proto.coefficient_imag = term.coefficient.imag
for t in sorted(term.items()): # sort to keep qubits ordered.
pauliterm_proto.paulis.add(
qubit_id=op_serializer.qubit_to_proto(t[0]),
pauli_type=str(t[1]),
)
paulisum_proto.terms.extend([pauliterm_proto])
return paulisum_proto
def deserialize_paulisum(proto):
"""Constructs a `cirq.PauliSum` from pauli_sum proto.
Args:
proto: A pauli_sum proto object.
Returns:
A `cirq.PauliSum` object.
"""
if not isinstance(proto, pauli_sum_pb2.PauliSum):
raise TypeError("deserialize requires a pauli_sum_pb2 object."
" Given: " + str(type(proto)))
res = cirq.PauliSum()
for term_proto in proto.terms:
coef = float(_round(term_proto.coefficient_real)) + \
1.0j * float(_round(term_proto.coefficient_imag))
term = coef * cirq.PauliString()
for pauli_qubit_pair in term_proto.paulis:
op = _process_pauli_type(pauli_qubit_pair.pauli_type)
term *= op(
op_deserializer.qubit_from_proto(pauli_qubit_pair.qubit_id))
res += term
return res
def _process_pauli_type(char):
if char == 'Z':
return cirq.Z
if char == 'X':
return cirq.X
if char == 'Y':
return cirq.Y
raise ValueError("Invalid pauli type.")
def serialize_projectorsum(projectorsum):
"""Constructs a projector_sum proto from `cirq.ProjectorSum`.
Args:
projectorsum: A `cirq.ProjectorSum` or `cirq.ProjectorString` object.
Returns:
A projector_sum proto object.
"""
if isinstance(projectorsum, cirq.ProjectorString):
projectorsum = cirq.ProjectorSum.from_pauli_strings(projectorsum)
if not isinstance(projectorsum, cirq.ProjectorSum):