"examples/vscode:/vscode.git/clone" did not exist on "1b6c7ea74ed1c23f1144e4167a284d5d9cff18c5"
quantum_computing_abstract.h 18.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Copyright (C) 2008  Davis E. King (davisking@users.sourceforge.net)
// License: Boost Software License   See LICENSE.txt for the full license.
#undef DLIB_QUANTUM_COMPUTINg_ABSTRACT_
#ifdef DLIB_QUANTUM_COMPUTINg_ABSTRACT_

#include <complex>
#include "../matrix.h"
#include "../rand.h"

namespace dlib
{

// ----------------------------------------------------------------------------------------

    typedef std::complex<double> qc_scalar_type;

// ----------------------------------------------------------------------------------------

    class quantum_register
    {
        /*!
            INITIAL VALUE
                - num_bits() == 1
24
25
26
27
28
29
30
31
                - state_vector().nr() == 2
                - state_vector().nc() == 1
                - state_vector()(0) == 1
                - state_vector()(1) == 0
                - probability_of_bit(0) == 0

                - i.e. This register represents a single quantum bit and it is
                  completely in the 0 state.
32
33

            WHAT THIS OBJECT REPRESENTS
34
                This object represents a set of quantum bits.
35
36
37
38
39
40
        !*/

    public:

        quantum_register(
        );
41
42
43
44
        /*!
            ensures
                - this object is properly initialized
        !*/
45
46
47

        int num_bits (
        ) const;
48
49
50
51
        /*!
            ensures
                - returns the number of quantum bits in this register
        !*/
52
53

        void set_num_bits (
54
            int new_num_bits
55
        );
56
        /*!
Davis King's avatar
Davis King committed
57
58
            requires
                - 1 <= new_num_bits <= 30
59
60
61
62
63
64
65
            ensures
                - #num_bits() == new_num_bits
                - #state_vector().nr() == 2^new_num_bits
                  (i.e. the size of the state_vector is exponential in the number of bits in a register)
                - for all valid i:
                    - probability_of_bit(i) == 0
        !*/
66
67
68

        void zero_all_bits(
        );
69
70
71
72
73
        /*!
            ensures
                - for all valid i:
                    - probability_of_bit(i) == 0
        !*/
74
75
76
77

        void append ( 
            const quantum_register& reg
        );
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
        /*!
            ensures
                - #num_bits() == num_bits() + reg.num_bits()
                - #this->state_vector() == tensor_product(this->state_vector(), reg.state_vector())
                - The original bits in *this become the high order bits of the resulting 
                  register and all the bits in reg end up as the low order bits in the
                  resulting register.
        !*/

        double probability_of_bit (
            int bit
        ) const;
        /*!
            requires
                - 0 <= bit < num_bits()
            ensures
                - returns the probability of measuring the given bit and it being in the 1 state.
                - The returned value is also equal to the sum of norm(state_vector()(i)) for all
                  i where the bit'th bit in i is set to 1. (note that the lowest order bit is bit 0)
        !*/
98
99
100
101
102
103

        template <typename rand_type>
        bool measure_bit (
            int bit,
            rand_type& rnd
        );
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
        /*!
            requires
                - 0 <= bit < num_bits()
                - rand_type == an implementation of dlib/rand/rand_float_abstract.h
            ensures
                - measures the given bit in this register.  Let R denote the boolean
                  result of the measurement, where true means the bit was measured to
                  have value 1 and false means it had a value of 0.
                - if (R == true) then
                    - returns true
                    - #probability_of_bit(bit) == 1
                - else
                    - returns false
                    - #probability_of_bit(bit) == 0
        !*/
119
120
121
122
123
124
125
126

        template <typename rand_type>
        bool measure_and_remove_bit (
            int bit,
            rand_type& rnd
        );
        /*!
            requires
127
                - num_bits() > 1
128
                - 0 <= bit < num_bits()
129
                - rand_type == an implementation of dlib/rand/rand_float_abstract.h
130
            ensures
131
132
133
134
135
136
137
138
139
                - measures the given bit in this register.  Let R denote the boolean
                  result of the measurement, where true means the bit was measured to
                  have value 1 and false means it had a value of 0.
                - #num_bits() == num_bits() - 1
                - removes the bit that was measured from this register.
                - if (R == true) then
                    - returns true
                - else
                    - returns false
140
141
142
143
        !*/

        const matrix<qc_scalar_type,0,1>& state_vector(
        ) const;
144
145
146
147
148
        /*!
            ensures
                - returns a const reference to the state vector that describes the state of
                  the quantum bits in this register.
        !*/
149
150
151

        matrix<qc_scalar_type,0,1>& state_vector(
        );
152
153
154
155
156
        /*!
            ensures
                - returns a non-const reference to the state vector that describes the state of
                  the quantum bits in this register.
        !*/
157
158
159
160

        void swap (
            quantum_register& item
        );
161
162
163
164
        /*!
            ensures
                - swaps *this and item
        !*/
165
166
167
168
169
170
171

    };

    inline void swap (
        quantum_register& a,
        quantum_register& b
    ) { a.swap(b); }
172
173
174
    /*!
        provides a global swap function
    !*/
175
176
177
178
179
180
181
182

// ----------------------------------------------------------------------------------------

    template <typename T>
    class gate_exp
    {
        /*!
            REQUIREMENTS ON T
183
184
                T must be some object that inherits from gate_exp and implements its own
                version of operator() and compute_state_element().
185
186
187
188

            WHAT THIS OBJECT REPRESENTS
                This object represents an expression that evaluates to a quantum gate 
                that operates on T::num_bits qubits.
189
190
191
192
193

                This object makes it easy to create new types of gate objects. All
                you need to do is inherit from gate_exp in the proper way and 
                then you can use your new gate objects in conjunction with all the 
                others.
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
        !*/

    public:

        static const long num_bits = T::num_bits;
        static const long dims = T::dims; 

        gate_exp(
            T& exp
        );
        /*!
            ensures
                - #&ref() == &exp
        !*/

        const qc_scalar_type operator() (
            long r, 
            long c
        ) const;
        /*!
Davis King's avatar
Davis King committed
214
215
216
            requires
                - 0 <= r < dims
                - 0 <= c < dims
217
218
219
220
221
222
223
224
225
226
227
228
            ensures
                - returns ref()(r,c)
        !*/

        void apply_gate_to (
            quantum_register& reg
        ) const;
        /*!
            requires
                - reg.num_bits() == num_bits
            ensures
                - applies this quantum gate to the given quantum register
229
230
                - Let M represent the matrix for this quantum gate, then
                  #reg().state_vector() = M*reg().state_vector()
231
232
233
234
235
236
237
238
239
240
241
242
243
        !*/

        template <typename exp>
        qc_scalar_type compute_state_element (
            const matrix_exp<exp>& reg,
            long row_idx
        ) const;
        /*!
            requires
                - reg.nr() == dims
                - reg.nc() == 1
                - 0 <= row_idx < dims
            ensures
244
245
                - Let M represent the matrix for this gate, then   
                  this function returns rowm(M*reg, row_idx)
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
                  (i.e. returns the row_idx row of what you get when you apply this
                  gate to the given column vector in reg)
                - This function works by calling ref().compute_state_element(reg,row_idx)
        !*/

        const T& ref(
        );
        /*!
            ensures
                - returns a reference to the subexpression contained in this object
        !*/

    };

// ----------------------------------------------------------------------------------------

    template <typename T, typename U>
    class composite_gate : public gate_exp<composite_gate<T,U> >
    {
265
266
267
268
269
270
        /*!
            REQUIREMENTS ON T AND U
                Both must be gate expressions that inherit from gate_exp

            WHAT THIS OBJECT REPRESENTS
                This object represents a quantum gate that is the tensor product of 
271
                two other quantum gates.
272
273
274
275
276
277
278
279
280
281


                As an example, suppose you have 3 registers, reg_high, reg_low, and reg_all.  Also
                suppose that reg_all is what you get when you append reg_high and reg_low,
                so reg_all.state_vector() == tensor_product(reg_high.state_vector(),reg_low.state_vector()).
                
                Then applying a composite gate to reg_all would give you the same thing as
                applying the lhs gate to reg_high and the rhs gate to reg_low and then appending 
                the two resulting registers.
        !*/
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
    public:

        composite_gate (
            const composite_gate& g
        );
        /*!
            ensures
                - *this is a copy of g
        !*/

        composite_gate(
            const gate_exp<T>& lhs_, 
            const gate_exp<U>& rhs_
        ): 
        /*!
            ensures
                - #lhs == lhs_.ref()
                - #rhs == rhs_.ref()
                - #num_bits == T::num_bits + U::num_bits
                - #dims == 2^num_bits
                - #&ref() == this
        !*/

        const qc_scalar_type operator() (
            long r, 
            long c
        ) const; 
        /*!
            requires
                - 0 <= r < dims
                - 0 <= c < dims
            ensures
314
315
                - Let M denote the tensor product of lhs with rhs, then this function
                  returns M(r,c)
316
317
318
319
320
321
322
323
324
325
326
327
328
329
                  (i.e. returns lhs(r/U::dims,c/U::dims)*rhs(r%U::dims, c%U::dims))
        !*/

        template <typename exp>
        qc_scalar_type compute_state_element (
            const matrix_exp<exp>& reg,
            long row_idx
        ) const;
        /*!
            requires
                - reg.nr() == dims
                - reg.nc() == 1
                - 0 <= row_idx < dims
            ensures
330
331
                - Let M represent the matrix for this gate, then this function
                  returns rowm(M*reg, row_idx)
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
                  (i.e. returns the row_idx row of what you get when you apply this
                  gate to the given column vector in reg)
                - This function works by calling rhs.compute_state_element() and using elements
                  of the matrix in lhs.  
        !*/

        static const long num_bits;
        static const long dims;

        const T lhs;
        const U rhs;
    };

// ----------------------------------------------------------------------------------------

    template <long bits>
    class gate : public gate_exp<gate<bits> >
    {
        /*!
            REQUIREMENTS ON bits
                0 < bits <= 30

            WHAT THIS OBJECT REPRESENTS
355
356
                This object represents a quantum gate that operates on bits qubits. 
                It stores its gate matrix explicitly in a dense in-memory matrix. 
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
        !*/

    public:
        gate(
        );
        /*!
            ensures
                - num_bits == bits
                - dims == 2^bits
                - #&ref() == this
                - for all valid r and c:
                    #(*this)(r,c) == 0
        !*/

        gate (
            const gate& g
        );
        /*!
            ensures
                - *this is a copy of g
        !*/

        template <typename T>
        explicit gate(
            const gate_exp<T>& g
        );
        /*!
            requires
                - T::num_bits == num_bits
            ensures
                - num_bits == bits
                - dims == 2^bits
                - #&ref() == this
                - for all valid r and c:
                    #(*this)(r,c) == g(r,c)
        !*/

        const qc_scalar_type& operator() (
            long r, 
            long c
397
398
399
400
401
402
        ) const;
        /*!
            requires
                - 0 <= r < dims
                - 0 <= c < dims
            ensures
403
404
                - Let M denote the matrix for this gate, then this function
                  returns a const reference to M(r,c)
405
        !*/
406
407
408
409

        qc_scalar_type& operator() (
            long r, 
            long c
410
411
412
413
414
415
        );
        /*!
            requires
                - 0 <= r < dims
                - 0 <= c < dims
            ensures
416
417
                - Let M denote the matrix for this gate, then this function 
                  returns a non-const reference to M(r,c)
418
        !*/
419
420
421
422
423
424
425
426
427
428
429
430

        template <typename exp>
        qc_scalar_type compute_state_element (
            const matrix_exp<exp>& reg,
            long row_idx
        ) const;
        /*!
            requires
                - reg.nr() == dims
                - reg.nc() == 1
                - 0 <= row_idx < dims
            ensures
431
432
                - Let M represent the matrix for this gate, then this function
                  returns rowm(M*reg, row_idx)
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
                  (i.e. returns the row_idx row of what you get when you apply this
                  gate to the given column vector in reg)
        !*/

        static const long num_bits;
        static const long dims;

    };

// ----------------------------------------------------------------------------------------

    template <typename T, typename U>
    const composite_gate<T,U> operator, ( 
        const gate_exp<T>& lhs,
        const gate_exp<U>& rhs
    ) { return composite_gate<T,U>(lhs,rhs); }
    /*!
450
451
452
        ensures
            - returns a composite_gate that represents the tensor product of the lhs
              gate with the rhs gate.
453
454
455
456
457
458
459
460
    !*/

// ----------------------------------------------------------------------------------------

    namespace quantum_gates
    {

        inline const gate<1> hadamard(
461
462
463
464
465
466
467
468
        );
        /*!
            ensures
                - returns the Hadamard gate.
                  (i.e. A gate with a matrix of
                                 |1, 1|
                     1/sqrt(2) * |1,-1|   )
        !*/
469
470

        inline const gate<1> x(
471
472
473
474
475
476
477
478
        );
        /*!
            ensures
                - returns the not gate.
                  (i.e. A gate with a matrix of
                      |0, 1|
                      |1, 0|   )
        !*/
479
480

        inline const gate<1> y(
481
482
483
484
485
486
487
488
        );
        /*!
            ensures
                - returns the y gate.
                  (i.e. A gate with a matrix of
                      |0,-i|
                      |i, 0|   )
        !*/
489
490

        inline const gate<1> z(
491
492
493
494
495
496
497
498
        );
        /*!
            ensures
                - returns the z gate.
                  (i.e. A gate with a matrix of
                      |1, 0|
                      |0,-1|   )
        !*/
499
500

        inline const gate<1> noop(
501
502
503
504
505
506
507
508
        );
        /*!
            ensures
                - returns the no-op or identity gate.
                  (i.e. A gate with a matrix of
                      |1, 0|
                      |0, 1|   )
        !*/
509

510
511
512
513
        template <
            int control_bit,
            int target_bit
            >
514
515
        class cnot : public gate_exp<cnot<control_bit, target_bit> >
        {
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
            /*!
                REQUIREMENTS ON control_bit AND target_bit
                    - control_bit != target_bit

                WHAT THIS OBJECT REPRESENTS
                    This object represents the controlled-not quantum gate.  It is a gate that
                    operates on abs(control_bit-target_bit)+1 qubits.   

                    In terms of the computational basis vectors, this gate maps input
                    vectors to output vectors in the following way:
                        - if (the input vector corresponds to a state where the control_bit
                          qubit is 1) then
                            - this gate outputs the computational basis vector that
                              corresponds to the state where the target_bit has been flipped
                              with respect to the input vector
                        - else
                            - this gate outputs the input vector unmodified

            !*/
535
536
        };

537
538
539
540
541
542
        template <
            int control_bit1,
            int control_bit2,
            int target_bit
            >
        class toffoli : public gate_exp<toffoli<control_bit1, control_bit2, target_bit> >
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
            /*!
                REQUIREMENTS ON control_bit1, control_bit2, AND target_bit
                    - all the arguments denote different bits, i.e.:
                        - control_bit1 != target_bit
                        - control_bit2 != target_bit
                        - control_bit1 != control_bit2
                    - The target bit can't be in-between the control bits, i.e.:
                        - (control_bit1 < target_bit && control_bit2 < target_bit) ||
                          (control_bit1 > target_bit && control_bit2 > target_bit) 

                WHAT THIS OBJECT REPRESENTS
                    This object represents the toffoli variant of a controlled-not quantum gate.  
                    It is a gate that operates on max(abs(control_bit2-target_bit),abs(control_bit1-target_bit))+1 
                    qubits.   

                    In terms of the computational basis vectors, this gate maps input
                    vectors to output vectors in the following way:
                        - if (the input vector corresponds to a state where the control_bit1 and
                          control_bit2 qubits are 1) then
                            - this gate outputs the computational basis vector that
                              corresponds to the state where the target_bit has been flipped
                              with respect to the input vector
                        - else
                            - this gate outputs the input vector unmodified

            !*/
570
571
572
573
574
575
576
577
578
579
580
581
582
583
        };

    // ------------------------------------------------------------------------------------

    }

// ----------------------------------------------------------------------------------------

}

#endif // DLIB_QUANTUM_COMPUTINg_ABSTRACT_