-
Notifications
You must be signed in to change notification settings - Fork 232
Expand file tree
/
Copy pathdemo.py
More file actions
1435 lines (1243 loc) · 57.8 KB
/
demo.py
File metadata and controls
1435 lines (1243 loc) · 57.8 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
r""".. role:: html(raw)
:format: html
Quantum just-in-time compiling Shor's algorithm with Catalyst
=============================================================
The past few years stimulated a lot of discussion about *hybrid
quantum-classical algorithms*. For a time, this terminology was synonymous
with *variational algorithms*. However, integration with classical
coprocessors is necessary for every quantum algorithm, even ones considered
quintessentially quantum.
.. figure:: ../_static/demo_thumbnails/opengraph_demo_thumbnails/OGthumbnail_shor_algorithm_catalyst_pennylane.png
:align: center
:width: 70%
:target: javascript:void(0)
Shor's famous factoring algorithm [#Shor1997]_ is one such example. Consider
an integer :math:`N`, promised to be the product of two primes, :math:`p` and
:math:`q`. Shor's algorithm uses a quantum computer to solve this problem with
exponentially-better scaling than the best-known classical algorithm. But,
have a look at the code below:
"""
import jax.numpy as jnp
def shors_algorithm(N):
p, q = 0, 0
while p * q != N:
a = jnp.random.choice(jnp.arange(2, N - 1))
if jnp.gcd(N, a) != 1:
p = jnp.gcd(N, a)
return p, N // p
guess_r = guess_order(N, a)
if guess_r % 2 == 0:
guess_square_root = (a ** (guess_r // 2)) % N
if guess_square_root not in [1, N - 1]:
p = jnp.gcd(N, guess_square_root - 1)
q = jnp.gcd(N, guess_square_root + 1)
return p, q
######################################################################
# If you saw this code out of context, would it even occur to you that it's for
# a quantum algorithm? There are no quantum circuits in sight, and the only "q"
# is a variable name!
#
# As quantum hardware continues to scale up, the way we reason about quantum
# programming is evolving in tandem. Writing circuits gate-by-gate for
# algorithms with hundreds or thousands of qubits is unsustainable. More
# fundamentally, we should consider whether programmers really need deep
# knowledge of quantum computing at all, if the software library can generate
# and compile appropriate quantum code (though, they should probably have at
# least some awareness, since the output of ``guess_order`` is probabilistic!).
# This raises the important questions of what gets compiled, and where, when,
# and how compilation happens.
#
# In PennyLane, classical and quantum code can be compiled *together*, as a
# single workflow, using the `Catalyst
# <https://docs.pennylane.ai/projects/catalyst/en/latest/index.html>`_
# library. This demo leverages their integration to implement Shor's factoring
# algorithm using just-in-time compilation from beginning to end, i.e., classical
# control structure and all. Even better, compilation happens only once per
# distinct *bit-width* of the factored integers, which can lead to huge savings
# in compilation time for realistic problem sizes.
#
# Compilation
# -----------
#
# Classical compilation
# ^^^^^^^^^^^^^^^^^^^^^
#
# Compilation is the process of translating operations expressed in a high-level
# language to a low-level language. In compiled languages like C and C++, compilation
# happens offline prior to code execution. A compiler takes a program as input
# and sends it through a sequence of *passes* that perform tasks such as syntax
# analysis, code generation, and optimization. The compiler outputs a new
# program in assembly code, which gets passed to an assembler. The assembler
# translates this code into a machine-executable program that we can feed inputs
# to, then run [#PurpleDragonBook]_.
#
# Compilation is not the only way to execute a program. Python, for example, is
# an *interpreted* language. Both a source program and inputs are fed to the
# interpreter, which processes them line by line and directly yields the program
# output [#PurpleDragonBook]_.
#
# Compilation and interpretation each have strengths and weaknesses. Compilation
# generally leads to faster execution, because optimizations can consider
# the overall structure of a program. However, the executable code is not
# human-readable and thus harder to debug. Interpretation is slower, but
# debugging is often easier because execution can be paused to inspect
# the program state or view diagnostic information [#PurpleDragonBook]_.
#
# In between these extremes lies *just-in-time compilation*. Just-in-time (JIT)
# compilation happens *during* execution. If a programmer specifies a function
# should be JIT compiled, the first time the interpreter sees it, it will spend
# a little more time to construct and cache a compiled version of that
# function. The next time that function is executed, the compiled version can be
# reused, provided the structure of the inputs hasn't changed [#JAXJIT]_.
#
# .. figure:: ../_static/demonstration_assets/shor_catalyst/compilation_comparison.png
# :scale: 75%
# :align: center
# :alt: The spectrum of compilation techniques: ahead-of-time, just-in-time, and interpretation.
#
# The spectrum of compilation techniques, from ahead-of-time to interpretation.
#
# Quantum compilation
# ^^^^^^^^^^^^^^^^^^^
# Quantum compilation, like its classical counterpart, lowers an algorithm from
# high-level instructions to low-level instructions. The bulk of
# this process involves converting a circuit expressed as generic, abstract gates
# to a sequence of gates that satisfy the constraints of a particular
# hardware device. Quantum compilation also involves multiple passes through
# one or more intermediate representations, and both
# machine-independent and dependent optimizations, as depicted below.
#
# .. figure:: ../_static/demonstration_assets/shor_catalyst/compilation-stack.svg
# :scale: 75%
# :align: center
# :alt: The quantum compilation stack.
#
# High-level overview of the quantum compilation stack. Each step contains
# numerous subroutines and passes of its own, and many require solving
# computationally hard problems (or very good heuristic techniques).
#
# Developing automated compilation tools is an active area of quantum software
# research. Even if a library contains built-in implementations of quantum
# circuits (e.g., the Fourier transform, or a multi-controlled operation),
# without a proper compiler a user would be left to optimize and map them to
# hardware by hand. This is a laborious (and error-prone!) process, and
# furthermore, is unlikely to be optimal.
#
# Suppose we want to compile and optimize quantum circuits for Shor's algorithm
# to factor an integer :math:`N`. Recalling the pseudocode above, let's break
# the algorithm down into a few distinct steps to identify where quantum
# compilation happens (for a full description of Shor's algorithm, the
# interested reader is referred to the `PennyLane Codebook
# <https://pennylane.ai/codebook/10-shors-algorithm/>`_).
#
# - Randomly select an integer, :math:`a`, between 2 and
# :math:`N-1` (double check we didn't get lucky and :math:`a` has a common factor with :math:`N`)
# - Using :math:`N` and :math:`a`, generate a circuit for *order finding* on a quantum computer. Execute it, and use the measurement results to obtain a candidate non-trivial square root
# - If the square root is non-trivial, test for valid factors. Otherwise, take more measurement shots, or try a different :math:`a`.
#
# The key thing to note is that for every :math:`N` and :math:`a`, a different
# quantum circuit must be generated, compiled and optimized. Even with a good
# compiler, this will lead to a huge computational overhead! Recall also that in
# a cryptographic context, :math:`N` relates to a public key that is unique for
# every entity. Moreover, for sizes of cryptographic relevance, :math:`N` will
# be a 2048-bit integer (or larger)! It would be ideal if we could reuse and
# share some of this work across different :math:`a` and :math:`N`. To that end,
# JIT compilation is a worthwhile option to explore.
#
# Quantum just-in-time compilation
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#
# In standard PennyLane, quantum circuit execution can be JIT compiled with
# JAX. To learn more, check out the JAX documentation [#JAXJIT]_ and the
# :doc:`PennyLane demo <demos/tutorial_jax_transformations>` on the
# subject. But this only compiles a single circuit. What about all the other
# code around it? What if you also wanted to optimize that quantum circuit,
# based on contextual information?
#
# This is where Catalyst comes in. Catalyst enables quantum JIT (QJIT)
# compilation of the *entire* algorithm from beginning to end. On the surface, it
# looks to be as simple as the following:
import pennylane as qp
@qp.qjit
def shors_algorithm(N):
# Implementation goes here
return p, q
######################################################################
# In practice, it is not so simple, and requires some special-purpose functions
# and data manipulation. But ultimately, we will show how to QJIT the most
# important parts such that the signature can be as minimal as this:
@qp.qjit(autograph=True, static_argnums=(1))
def shors_algorithm(N, n_bits):
# Implementation goes here
return p, q
######################################################################
# Furthermore, along the way we'll leverage the structure of :math:`a` to
# construct more optimal quantum circuits in the QJITted function.
#
# QJIT compiling Shor's algorithm
# -------------------------------
#
# Quantum subroutines
# ^^^^^^^^^^^^^^^^^^^
#
# The implementation of the classical portion of Shor's algorithm is
# near-identical to the pseudocode at the beginning, and can be JIT compiled
# essentially as-is. The quantum aspects are where challenges arise.
#
# This section outlines the quantum circuits used in the order-finding
# subroutine. The presented implementation is based on that of Beauregard
# [#Beauregard2003]_. For an integer :math:`N` with an :math:`n = \lceil \log_2
# N \rceil`-bit representation, we require :math:`2n + 3` qubits, where :math:`n
# + 1` are for computation and :math:`n + 2` are auxiliary.
#
# Order finding is an application of *quantum phase estimation*
# (:doc:`QPE <demos/tutorial_qpe>`) for the operator
#
# .. math::
#
# U_a \vert x \rangle = \vert ax \pmod N \rangle,
#
# where :math:`\vert x \rangle` is the binary representation of integer
# :math:`x`, and :math:`a` is the randomly-generated integer discussed
# above. The full QPE circuit for producing a :math:`t`-bit estimate of a
# phase, :math:`\theta`, is presented below.
#
# .. figure:: ../_static/demonstration_assets/shor_catalyst/qpe_full.svg
# :width: 600
# :align: center
# :alt: Quantum phase estimation circuit for order finding.
#
# Initial circuit for order finding with quantum phase estimation (QPE).
#
# This high-level view hides the circuit's complexity; the implementation
# details of :math:`U_a` are not shown, and auxiliary qubits are omitted. In
# what follows, we'll leverage shortcuts afforded by Catalyst and the hybrid
# nature of computation. Specifically, with mid-circuit measurement and reset, we
# can reduce the number of estimation wires to :math:`t=1`. Most arithmetic will
# be performed in the Fourier basis. Finally, with Catalyst, we can vary circuit
# structure based on :math:`a` to save resources, *even though its value isn't
# known until runtime*.
#
# First, we'll use our classical knowledge of :math:`a` to simplify the
# implementation of the controlled :math:`U_a^{2^k}`. Naively, it looks like we
# must apply the controlled :math:`U_a` operations :math:`2^k` times. However,
# note
#
# .. math::
#
# U_a^{2^k}\vert x \rangle = \vert (a \cdot a \cdots a) x \pmod N \rangle = \vert a^{2^k}x \pmod N \rangle = U_{a^{2^k}} \vert x \rangle.
#
# Since :math:`a` is known, we can classically evaluate :math:`a^{2^k} \pmod
# N` and implement controlled-:math:`U_{a^{2^k}}` instead.
#
# .. figure:: ../_static/demonstration_assets/shor_catalyst/qpe_full_modified_power.svg
# :width: 600
# :align: center
# :alt: Order finding with controlled operations that take advantage of classical precomputation.
#
# Leveraging knowledge of :math:`a` allows us to precompute powers for the
# controlled :math:`U_a`.
#
# However, there is a tradeoff. The implementation of :math:`U_{a^{2^k}}` will
# be different for each power of :math:`a`, so we
# must compile and optimize more than one circuit. On the other hand, we now run
# only :math:`t` controlled operations instead of :math:`1 + 2 + 4 + \cdots +
# 2^{t-1} = 2^t - 1`. The additional compilation time is likely to be outweighed
# by the reduced number of function calls, especially if we can JIT compile the
# circuit construction.
#
# Next, let's zoom in on an arbitrary controlled :math:`U_a`.
#
# .. figure:: ../_static/demonstration_assets/shor_catalyst/c-ua.svg
# :width: 700
# :align: center
# :alt: Quantum phase estimation circuit for order finding.
#
# Implementing a controlled :math:`U_a` with modular arithmetic
# [#Beauregard2003]_.
#
# The control qubit, :math:`\vert c\rangle`, is an estimation qubit. The
# register :math:`\vert x \rangle` and the auxiliary register contain :math:`n`
# and :math:`n + 1` qubits respectively, for reasons described below.
#
# :math:`M_a` multiplies the contents of one register by :math:`a` and adds it to
# another register, in place and modulo :math:`N`,
#
# .. math::
#
# M_a \vert x \rangle \vert b \rangle \vert 0 \rangle = \vert x \rangle \vert (b + ax) \pmod N \rangle \vert 0 \rangle.
#
# Ignoring the control qubit, we can validate that this circuit implements
# :math:`U_a`:
#
# .. math::
#
# \begin{eqnarray}
# M_a \vert x \rangle \vert 0 \rangle^{\otimes n + 1} \vert 0 \rangle &=& \vert x \rangle \vert ax \rangle \vert 0 \rangle \\
# SWAP (\vert x \rangle \vert ax \rangle ) \vert 0 \rangle &=& \vert ax \rangle \vert x \rangle \vert 0 \rangle \\
# M_{a^{-1}}^\dagger \vert ax \rangle \vert x \rangle \vert 0 \rangle &=& \vert ax\rangle \vert x - a^{-1}(ax) \rangle \vert 0 \rangle \\
# &=& \vert ax \rangle \vert 0 \rangle^{\otimes n + 1} \vert 0 \rangle,
# \end{eqnarray}
#
# where we've omitted the "mod :math:`N`" for readability, and used the fact
# that the adjoint of addition is subtraction.
#
# A high-level implementation of a controlled :math:`M_a` is shown below.
#
# .. figure:: ../_static/demonstration_assets/shor_catalyst/doubly-controlled-adder-with-control.svg
# :width: 700
# :align: center
# :alt: Controlled addition of :math:`ax` using a series of double-controlled Fourier adders.
#
# Circuit for controlled multiplication of :math:`ax`. The circuit element labeled :math:`\Phi_+`
# performs addition modulo :math:`N` in the Fourier basis (see main text for a full description)
# [#Beauregard2003]_.
#
# First, note that the controls on the quantum Fourier transforms (QFTs) are not
# needed. If we remove them and :math:`\vert c \rangle = \vert 1 \rangle`, the
# circuit works as expected. If :math:`\vert c \rangle = \vert 0 \rangle`, they
# run, but cancel each other out since none of the operations in between will
# execute (this optimization is broadly applicable to controlled operations, and
# quite useful!).
#
# .. figure:: ../_static/demonstration_assets/shor_catalyst/doubly-controlled-adder-with-control-not-on-qft.svg
# :width: 700
# :align: center
# :alt: Controlled addition of :math:`ax` using a series of double-controlled Fourier adders, controls on QFT removed.
#
# The controls on the QFT can be removed without altering the effect of the circuit
# [#Beauregard2003]_.
#
# At first glance, it's not clear how this produces :math:`a x`. The qubits in
# register :math:`\vert x \rangle` control operations that depend on :math:`a`
# multiplied by various powers of 2. There is a QFT before and after, whose
# purpose is unclear, and we have yet to define the gate labelled :math:`\Phi_+`.
#
# The operation, :math:`\Phi_+`, performs addition modulo :math:`N` in the
# *Fourier basis* [#Draper2000]_. This is another trick that leverages
# prior knowledge of :math:`a`. Rather than performing addition on bits in
# computational basis states, we apply a QFT, adjust the phases based on the
# bits of the number being added, and then an inverse QFT to obtain the result
# in the computational basis.
#
# To understand how Fourier addition works, let's begin with the simpler case of
# non-modular addition. The regular addition circuit, denoted by :math:`\Phi`,
# is shown below.
#
# .. figure:: ../_static/demonstration_assets/shor_catalyst/fourier_adder.svg
# :width: 750
# :align: center
# :alt: Addition in the Fourier basis.
#
# Circuit for addition in the Fourier basis [#Draper2000]_. Calligraphic
# letters indicate basis states converted to the Fourier basis.
#
# Fourier addition of two :math:`n`-bit numbers uses :math:`n+1` qubits, as it
# accounts for the possibility of *overflow* during addition (this constitutes one
# of our auxiliary qubits). The :math:`\mathbf{R}_k` are rotations that depend
# on the binary representation of :math:`a`,
#
# .. math::
#
# \mathbf{R}_k = \begin{pmatrix} 1 & 0 \\ 0 & e^{2\pi i\sum_{\ell=0}^{k} \frac{a_\ell}{2^{\ell+1}}} \end{pmatrix}.
#
# A detailed derivation of this circuit is included in the
# :ref:`Appendix <appendix_fourier_adder>`.
#
# Next, we must augment Fourier addition to work modulo :math:`N` (i.e.,
# :math:`\Phi_+`). This can be done by adding an auxiliary qubit and a sequence
# of operations to compensate for any overflow that occurs during addition. A
# full explanation of Fourier addition modulo :math:`N` is also provided in the
# :ref:`Appendix <appendix_fourier_adder_modulo_n>`.
#
# .. figure:: ../_static/demonstration_assets/shor_catalyst/fourier_adder_modulo_n.svg
# :width: 800
# :align: center
# :alt: Addition in the Fourier basis modulo N.
#
# Circuit for doubly-controlled Fourier addition modulo :math:`N`
# [#Beauregard2003]_. Calligraphic letters indicate basis states converted to
# the Fourier basis.
#
# This completes our implementation of the controlled :math:`U_{a^{2^k}}`. The
# full circuit, shown below, uses :math:`t + 2n + 2` qubits.
#
# .. figure:: ../_static/demonstration_assets/shor_catalyst/qpe_full_combined_multi_est_wires.svg
# :width: 500
# :align: center
# :alt: Full QPE circuit, all t estimation wires, and decompositions.
#
# Initial circuit for order finding with QPE, and decompositions of its
# constituent subroutines.
#
#
# Taking advantage of classical information
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# Above, we incorporated information about :math:`a` in the controlled
# :math:`U_a` and the Fourier adder. In what follows, we identify a few other
# places where classical information can be leveraged.
#
# First, consider the controlled :math:`U_{a^{2^0}} = U_a` at the beginning of
# the algorithm. The only basis state this operation applies to is :math:`\vert
# 1 \rangle`, which gets mapped to :math:`\vert a \rangle`. This is effectively
# just controlled addition of :math:`a - 1` to :math:`1`. Since :math:`a`
# is selected from between :math:`2` and :math:`N-2` inclusive, the addition is
# guaranteed to never overflow. This means we can simply do a controlled Fourier
# addition, and save a significant number of resources!
#
# We can also make some optimizations to the end of the algorithm by keeping
# track of the powers of :math:`a`. If, at iteration :math:`k,` we have
# :math:`a^{2^k} = 1`, no further multiplication is necessary because we would
# be multiplying by :math:`1.` In fact, we can terminate the algorithm early because
# we've found the order of :math:`a` is simply :math:`2^k`.
#
# There are also less-trivial optimizations we can make. Consider the
# sequence of doubly-controlled adders in the controlled :math:`M_a`. Below, we
# show the initial instance where the auxiliary register is in state
# :math:`\vert 0 \rangle`.
#
# .. figure:: ../_static/demonstration_assets/shor_catalyst/doubly-controlled-adder-simplified.svg
# :width: 600
# :align: center
# :alt: The controlled multiplier circuit in the context of Shor's algorithm.
#
# The doubly-controlled adders are each controlled on an estimation qubit and a
# qubit in the target register. Consider the state of the system after the
# initial controlled-:math:`U_a` (or, rather, controlled addition of
# :math:`a-1`),
#
# .. math::
#
# \begin{equation*}
# \vert + \rangle ^{\otimes (t - 1)} \frac{1}{\sqrt{2}} \left( \vert 0 \rangle \vert 1 \rangle + \vert 1 \rangle \vert a \rangle \right).
# \end{equation*}
#
# The next controlled operation is controlled :math:`U_{a^2}`. Since the only
# two basis states present are :math:`\vert 1 \rangle` and :math:`\vert a
# \rangle`, the only doubly-controlled :math:`\Phi_+` that trigger are the first
# one (with the second control on the bottom-most qubit) and those controlled
# on qubits that are :math:`1` in the binary representation of :math:`a`. Thus,
# we only need doubly-controlled operations on qubits where the logical OR of
# the bit representations of :math:`1` and :math:`a` are :math:`1!` We present here an example
# for :math:`a = 5`.
#
# .. figure:: ../_static/demonstration_assets/shor_catalyst/doubly-controlled-adder-simplified-states.svg
# :width: 800
# :align: center
# :alt: Leveraging knowledge of :math:`a` to eliminate unnecessary doubly-controlled additions.
#
# Leveraging knowledge of :math:`a` to eliminate unnecessary doubly-controlled additions.
#
# Depending on :math:`a`, this could be major savings, especially at the
# beginning of the algorithm where very few basis states are involved. The same
# trick can be used after the controlled SWAPs, as demonstrated below.
#
# .. figure:: ../_static/demonstration_assets/shor_catalyst/c-ua-basis-states-optimization.svg
# :width: 600
# :align: center
# :alt: Removing doubly-controlled additions based on expected terms in the register superposition.
#
# Doubly-controlled operations can be removed from controlled multiplications
# by keeping track of basis states involved.
#
# Eventually we expect diminishing returns because each controlled
# :math:`U_{a^{2^k}}` contributes more terms to the superposition. Before the
# :math:`k-` th iteration, the control register contains a superposition of
# :math:`\{ \vert a^j \rangle \}, j = 0, \ldots, 2^{k - 1}` (inclusive), and after the
# controlled SWAPs, the relevant superposition is :math:`\{ \vert a^j \rangle \}, j =
# 2^{k-1}+1, \ldots, 2^{k} - 1`.
#
# The "single-qubit" QPE
# ~~~~~~~~~~~~~~~~~~~~~~
#
# The optimizations in the previous section focus on reducing the number of
# operations (circuit depth). The number of qubits (circuit width) can be reduced
# using a well-known trick for the QFT. Let's return to the QPE circuit and
# expand the final inverse QFT.
#
# .. figure:: ../_static/demonstration_assets/shor_catalyst/qpe_full_modified_power_with_qft.svg
# :width: 800
# :align: center
# :alt: QPE circuit with inverse QFT expanded.
#
# Consider the bottom estimation wire. After the final Hadamard, this qubit only
# applies controlled operations. Rather than preserving its state, we can make a
# measurement and apply rotations classically controlled on the outcome
# (essentially, the reverse of the deferred measurement process). The same can
# be done for the next estimation wire; rotations applied to the remaining
# estimation wires then depend on the previous two measurement outcomes. We can
# repeat this process for all remaining estimation wires. Moreover, we can
# simply reuse the *same* qubit for *all* estimation wires, provided we keep
# track of the measurement outcomes classically, and apply an appropriate
# :math:`RZ` rotation,
#
# .. math::
#
# \mathbf{M}_{k} = \begin{pmatrix} 1 & 0 \\ 0 & e^{-2\pi i\sum_{\ell=0}^{k} \frac{\theta_{\ell}}{2^{k + 2 - \ell}}} \end{pmatrix}.
#
# This allows us to reformulate the QPE algorithm as:
#
# .. figure:: ../_static/demonstration_assets/shor_catalyst/qpe_full_modified_power_with_qft-7.svg
# :width: 800
# :align: center
# :alt: QPE circuit with one estimation qubit.
#
# A step-by-step example is included in the :ref:`Appendix <appendix_single_qubit_qpe>`.
#
# The final Shor circuit
# ~~~~~~~~~~~~~~~~~~~~~~
# Replacing the controlled-:math:`U_a` with the subroutines derived above, we
# see Shor's algorithm requires :math:`2n + 3` qubits in total, as summarized in
# the graphic below.
#
# .. figure:: ../_static/demonstration_assets/shor_catalyst/qpe_full_combined.svg
# :width: 800
# :align: center
# :alt: Full implementation of QPE circuit.
#
# From this, we see some additional optimizations are possible. In particular,
# the QFT and inverse QFT are applied before and after each :math:`M_a` block,
# so we can remove the ones that occur between the different controlled
# :math:`U_a` (the only ones remaining are the very first and last, and
# those before and after the controlled SWAPs).
######################################################################
# Catalyst implementation
# ^^^^^^^^^^^^^^^^^^^^^^^
#
# Circuits in hand, we now present the full implementation of Shor's algorithm.
#
# First, we require some utility functions for modular arithmetic:
# exponentiation by repeated squaring (to avoid overflow when exponentiating
# large integers), and computation of inverses modulo :math:`N`. Note that the
# first can be done in regular Python with the built-in ``pow`` method. However,
# it is not JIT-compatible and there is no equivalent in JAX NumPy.
from jax import numpy as jnp
def repeated_squaring(a, exp, N):
"""QJIT-compatible function to determine (a ** exp) % N.
Source: https://en.wikipedia.org/wiki/Modular_exponentiation#Left-to-right_binary_method
"""
bits = jnp.array(jnp.unpackbits(jnp.array([exp]).view("uint8"), bitorder="little"))
total_bits_one = jnp.sum(bits)
result = jnp.array(1, dtype=jnp.int32)
x = jnp.array(a, dtype=jnp.int32)
idx, num_bits_added = 0, 0
while num_bits_added < total_bits_one:
if bits[idx] == 1:
result = (result * x) % N
num_bits_added += 1
x = (x**2) % N
idx += 1
return result
def modular_inverse(a, N):
"""QJIT-compatible modular multiplicative inverse routine.
Source: https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm#Modular_integers
"""
t = jnp.array(0, dtype=jnp.int32)
newt = jnp.array(1, dtype=jnp.int32)
r = jnp.array(N, dtype=jnp.int32)
newr = jnp.array(a, dtype=jnp.int32)
while newr != 0:
quotient = r // newr
t, newt = newt, t - quotient * newt
r, newr = newr, r - quotient * newr
if t < 0:
t = t + N
return t
######################################################################
# We also require a few helper functions for phase estimation.
def fractional_binary_to_float(sample):
"""Convert an n-bit sample [k1, k2, ..., kn] to a floating point
value using fractional binary representation,
k = (k1 / 2) + (k2 / 2 ** 2) + ... + (kn / 2 ** n)
"""
powers_of_two = 2 ** (jnp.arange(len(sample)) + 1)
return jnp.sum(sample / powers_of_two)
def as_integer_ratio(f):
"""QJIT compatible conversion of a floating point number to two 64-bit
integers such that their quotient equals the input to available precision.
"""
mantissa, exponent = jnp.frexp(f)
i = 0
while jnp.logical_and(i < 300, mantissa != jnp.floor(mantissa)):
mantissa = mantissa * 2.0
exponent = exponent - 1
i += 1
numerator = jnp.asarray(mantissa, dtype=jnp.int64)
denominator = jnp.asarray(1, dtype=jnp.int64)
abs_exponent = jnp.abs(exponent)
if exponent > 0:
num_to_return, denom_to_return = numerator << abs_exponent, denominator
else:
num_to_return, denom_to_return = numerator, denominator << abs_exponent
return num_to_return, denom_to_return
def phase_to_order(phase, max_denominator):
"""Given some floating-point phase, estimate integers s, r such that s / r =
phase. Uses a JIT-compatible re-implementation of Fraction.limit_denominator.
"""
numerator, denominator = as_integer_ratio(phase)
order = 0
if denominator <= max_denominator:
order = denominator
else:
p0, q0, p1, q1 = 0, 1, 1, 0
a = numerator // denominator
q2 = q0 + a * q1
while q2 < max_denominator:
p0, q0, p1, q1 = p1, q1, p0 + a * p1, q2
numerator, denominator = denominator, numerator - a * denominator
a = numerator // denominator
q2 = q0 + a * q1
k = (max_denominator - q0) // q1
bound1 = p0 + k * p1 / q0 + k * q1
bound2 = p1 / q1
loop_res = 0
if jnp.abs(bound2 - phase) <= jnp.abs(bound1 - phase):
loop_res = q1
else:
loop_res = q0 + k * q1
order = loop_res
return order
######################################################################
# Below, we have the implementations of the arithmetic circuits derived in the
# previous section.
import pennylane as qp
import catalyst
from catalyst import measure
catalyst.autograph_strict_conversion = True
def QFT(wires):
"""The standard QFT, redefined because the PennyLane one uses terminal SWAPs."""
shifts = jnp.array([2 * jnp.pi * 2**-i for i in range(2, len(wires) + 1)])
for i in range(len(wires)):
qp.Hadamard(wires[i])
for j in range(len(shifts) - i):
qp.ControlledPhaseShift(shifts[j], wires=[wires[(i + 1) + j], wires[i]])
def fourier_adder_phase_shift(a, wires):
"""Sends QFT(|b>) -> QFT(|b + a>)."""
n = len(wires)
a_bits = jnp.unpackbits(jnp.array([a]).view("uint8"), bitorder="little")[:n][::-1]
powers_of_two = jnp.array([1 / (2**k) for k in range(1, n + 1)])
phases = jnp.array([jnp.dot(a_bits[k:], powers_of_two[: n - k]) for k in range(n)])
for i in range(len(wires)):
if phases[i] != 0:
qp.PhaseShift(2 * jnp.pi * phases[i], wires=wires[i])
def doubly_controlled_adder(N, a, control_wires, wires, aux_wire):
"""Sends |c>|x>QFT(|b>)|0> -> |c>|x>QFT(|b + c x a) mod N>)|0>."""
qp.ctrl(fourier_adder_phase_shift, control=control_wires)(a, wires)
qp.adjoint(fourier_adder_phase_shift)(N, wires)
qp.adjoint(QFT)(wires)
qp.CNOT(wires=[wires[0], aux_wire])
QFT(wires)
qp.ctrl(fourier_adder_phase_shift, control=aux_wire)(N, wires)
qp.adjoint(qp.ctrl(fourier_adder_phase_shift, control=control_wires))(a, wires)
qp.adjoint(QFT)(wires)
qp.PauliX(wires=wires[0])
qp.CNOT(wires=[wires[0], aux_wire])
qp.PauliX(wires=wires[0])
QFT(wires)
qp.ctrl(fourier_adder_phase_shift, control=control_wires)(a, wires)
def controlled_ua(N, a, control_wire, target_wires, aux_wires, mult_a_mask, mult_a_inv_mask):
"""Sends |c>|x>|0> to |c>|ax mod N>|0> if c = 1.
The mask arguments allow for the removal of unnecessary double-controlled additions.
"""
n = len(target_wires)
# Apply double-controlled additions where bits of a can be 1.
for i in range(n):
if mult_a_mask[n - i - 1] > 0:
pow_a = (a * (2**i)) % N
doubly_controlled_adder(
N, pow_a, [control_wire, target_wires[n - i - 1]], aux_wires[:-1], aux_wires[-1]
)
qp.adjoint(QFT)(wires=aux_wires[:-1])
# Controlled SWAP the target and aux wires; note that the top-most aux wire
# is only to catch overflow, so we ignore it here.
for i in range(n):
qp.CNOT(wires=[aux_wires[i + 1], target_wires[i]])
qp.Toffoli(wires=[control_wire, target_wires[i], aux_wires[i + 1]])
qp.CNOT(wires=[aux_wires[i + 1], target_wires[i]])
# Adjoint of controlled multiplication with the modular inverse of a
a_mod_inv = modular_inverse(a, N)
QFT(wires=aux_wires[:-1])
for i in range(n):
if mult_a_inv_mask[i] > 0:
pow_a_inv = (a_mod_inv * (2 ** (n - i - 1))) % N
qp.adjoint(doubly_controlled_adder)(
N,
pow_a_inv,
[control_wire, target_wires[i]],
aux_wires[:-1],
aux_wires[-1],
)
######################################################################
# Finally, let's put it all together in the core portion of Shor's algorithm,
# under the ``@qp.qjit`` decorator.
from jax import random
# ``n_bits`` is a static argument because ``jnp.arange`` does not currently
# support dynamically-shaped arrays when jitted.
@qp.qjit(autograph=True, static_argnums=(3))
def shors_algorithm(N, key, a, n_bits, n_trials):
# If no explicit a is passed (denoted by a = 0), randomly choose a
# non-trivial value of a that does not have a common factor with N.
if a == 0:
while jnp.gcd(a, N) != 1:
key, subkey = random.split(key)
a = random.randint(subkey, (1,), 2, N - 1)[0]
est_wire = 0
target_wires = jnp.arange(n_bits) + 1
aux_wires = jnp.arange(n_bits + 2) + n_bits + 1
dev = qp.device("lightning.qubit", wires=2 * n_bits + 3)
@qp.set_shots(1)
@qp.qnode(dev)
def run_qpe():
meas_results = jnp.zeros((n_bits,), dtype=jnp.int32)
cumulative_phase = jnp.array(0.0)
phase_divisors = 2.0 ** jnp.arange(n_bits + 1, 1, -1)
a_mask = jnp.zeros(n_bits, dtype=jnp.int64)
a_mask = a_mask.at[0].set(1) + jnp.array(
jnp.unpackbits(jnp.array([a]).view("uint8"), bitorder="little")[:n_bits]
)
a_inv_mask = a_mask
# Initialize the target register of QPE in |1>
qp.PauliX(wires=target_wires[-1])
# The "first" QFT on the auxiliary register; required here because
# QFT are largely omitted in the modular arithmetic routines due to
# cancellation between adjacent blocks of the algorithm.
QFT(wires=aux_wires[:-1])
# First iteration: add a - 1 using the Fourier adder.
qp.Hadamard(wires=est_wire)
QFT(wires=target_wires)
qp.ctrl(fourier_adder_phase_shift, control=est_wire)(a - 1, target_wires)
qp.adjoint(QFT)(wires=target_wires)
# Measure the estimation wire and store the phase
qp.Hadamard(wires=est_wire)
meas_results[0] = measure(est_wire, reset=True)
cumulative_phase = -2 * jnp.pi * jnp.sum(meas_results / jnp.roll(phase_divisors, 1))
# For subsequent iterations, determine powers of a, and apply controlled
# U_a when the power is not 1. Unnecessary double-controlled operations
# are removed, based on values stored in the two "mask" variables.
powers_cua = jnp.array([repeated_squaring(a, 2**p, N) for p in range(n_bits)])
loop_bound = n_bits
if jnp.min(powers_cua) == 1:
loop_bound = jnp.argmin(powers_cua)
# The core of the QPE routine
for pow_a_idx in range(1, loop_bound):
pow_cua = powers_cua[pow_a_idx]
if not jnp.all(a_inv_mask):
for power in range(2**pow_a_idx, 2 ** (pow_a_idx + 1)):
next_pow_a = jnp.array([repeated_squaring(a, power, N)])
a_inv_mask = a_inv_mask + jnp.array(
jnp.unpackbits(next_pow_a.view("uint8"), bitorder="little")[:n_bits]
)
qp.Hadamard(wires=est_wire)
controlled_ua(N, pow_cua, est_wire, target_wires, aux_wires, a_mask, a_inv_mask)
a_mask = a_mask + a_inv_mask
a_inv_mask = jnp.zeros_like(a_inv_mask)
# Rotate the estimation wire by the accumulated phase, then measure and reset it
qp.PhaseShift(cumulative_phase, wires=est_wire)
qp.Hadamard(wires=est_wire)
meas_results[pow_a_idx] = measure(est_wire, reset=True)
cumulative_phase = (
-2 * jnp.pi * jnp.sum(meas_results / jnp.roll(phase_divisors, pow_a_idx + 1))
)
# The adjoint partner of the QFT from the beginning, to reset the auxiliary register
qp.adjoint(QFT)(wires=aux_wires[:-1])
return meas_results
# The "classical" part of Shor's algorithm: run QPE, extract a candidate
# order, then check whether a valid solution is found. We run multiple trials,
# and return a success probability.
p, q = jnp.array(0, dtype=jnp.int32), jnp.array(0, dtype=jnp.int32)
successful_trials = jnp.array(0, dtype=jnp.int32)
for _ in range(n_trials):
sample = run_qpe()
phase = fractional_binary_to_float(sample)
guess_r = phase_to_order(phase, N)
# If the guess order is even, we may have a non-trivial square root.
# If so, try to compute p and q.
if guess_r % 2 == 0:
guess_square_root = repeated_squaring(a, guess_r // 2, N)
if guess_square_root != 1 and guess_square_root != N - 1:
candidate_p = jnp.gcd(guess_square_root - 1, N).astype(jnp.int32)
if candidate_p != 1:
candidate_q = N // candidate_p
else:
candidate_q = jnp.gcd(guess_square_root + 1, N).astype(jnp.int32)
if candidate_q != 1:
candidate_p = N // candidate_q
if candidate_p * candidate_q == N:
p, q = candidate_p, candidate_q
successful_trials += 1
return p, q, key, a, successful_trials / n_trials
######################################################################
# Let's ensure the algorithm can successfully factor a small case, :math:`N =
# 15`. We will randomly generate :math:`a` within the function, and do 100 shots
# of the phase estimation procedure to get an idea of the success probability.
key = random.PRNGKey(123456789)
N = 15
n_bits = int(jnp.ceil(jnp.log2(N)))
p, q, _, a, success_prob = shors_algorithm(N, key.astype(jnp.uint32), 0, n_bits, 100)
print(f"Found {N} = {p} x {q} (using random a = {a}) with probability {success_prob:.2f}")
######################################################################
# Performance and validation
# ^^^^^^^^^^^^^^^^^^^^^^^^^^
#
# Next, let's verify QJIT compilation is happening properly. We will run the
# algorithm for different :math:`N` with the same bit width, and different values of
# :math:`a`. We expect the first execution, for the very first :math:`N` and :math:`a`, to
# take longer than the rest.
import time
import matplotlib.pyplot as plt
# Some 6-bit numbers
N_values = [33, 39, 51, 55, 57]
n_bits = int(jnp.ceil(jnp.log2(N_values[0])))
num_a = 3
execution_times = []
key = random.PRNGKey(1010101)
for N in N_values:
unique_a = []
while len(unique_a) != num_a:
key, subkey = random.split(key.astype(jnp.uint32))
a = random.randint(subkey, (1,), 2, N - 1)[0]
if jnp.gcd(a, N) == 1 and a not in unique_a:
unique_a.append(a)
for a in unique_a:
start = time.time()
p, q, key, _, _ = shors_algorithm(N, key.astype(jnp.uint32), a, n_bits, 1)
end = time.time()
execution_times.append((N, a, end - start))
labels = [f"{ex[0]}, {int(ex[1])}" for ex in execution_times]
times = [ex[2] for ex in execution_times]
plt.scatter(range(len(times)), times, c=[ex[0] for ex in execution_times])
plt.xticks(range(len(times)), labels=labels, rotation=80)
plt.xlabel("N, a")
plt.ylabel("Runtime (s)")
plt.tight_layout()
plt.show()
######################################################################
# This plot demonstrates exactly what we suspect: changing :math:`N` and
# :math:`a` does not lead to recompilation of the program! This will be
# particularly valuable for large :math:`N`, where traditional circuit
# processing times can grow very large.
#
# To show this more explicitly, let's fix :math:`a = 2`, and generate Shor
# circuits for many different :math:`N` using both the QJIT version, and the
# plain PennyLane version below. Note the standard PennyLane version makes use
# of many of the same subroutines and optimizations, but due to limitations on
# how PennyLane handles mid-circuit measurements, we must use ``qp.cond`` and
# explicit ``qp.PhaseShift`` gates.
def shors_algorithm_no_qjit(N, key, a, n_bits, n_trials):
est_wire = 0
target_wires = list(range(1, n_bits + 1))
aux_wires = list(range(n_bits + 1, 2 * n_bits + 3))
dev = qp.device("lightning.qubit", wires=2 * n_bits + 3)
@qp.set_shots(1)
@qp.qnode(dev)
def run_qpe():
a_mask = jnp.zeros(n_bits, dtype=jnp.int64)
a_mask = a_mask.at[0].set(1) + jnp.array(
jnp.unpackbits(jnp.array([a]).view("uint8"), bitorder="little")[:n_bits]
)
a_inv_mask = a_mask
measurements = []
qp.PauliX(wires=target_wires[-1])
QFT(wires=aux_wires[:-1])
qp.Hadamard(wires=est_wire)
QFT(wires=target_wires)
qp.ctrl(fourier_adder_phase_shift, control=est_wire)(a - 1, target_wires)
qp.adjoint(QFT)(wires=target_wires)
qp.Hadamard(wires=est_wire)