Commit b58c05f5 authored by Davis King's avatar Davis King

Fixed a bug in the measurement functions and also cleaned up the spec.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402687
parent 04a5f724
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#define DLIB_QUANTUM_COMPUTINg_1_ #define DLIB_QUANTUM_COMPUTINg_1_
#include <complex> #include <complex>
#include <cmath>
#include "../matrix.h" #include "../matrix.h"
#include "../rand.h" #include "../rand.h"
#include "../enable_if.h" #include "../enable_if.h"
...@@ -131,6 +132,8 @@ namespace dlib ...@@ -131,6 +132,8 @@ namespace dlib
for (int i = 0; i < bit; ++i) for (int i = 0; i < bit; ++i)
mask <<= 1; mask <<= 1;
// loop over all the elements in the state vector and zero out those that
// conflict with the measurement we just made.
for (long r = 0; r < state.nr(); ++r) for (long r = 0; r < state.nr(); ++r)
{ {
const unsigned long field = r; const unsigned long field = r;
...@@ -147,7 +150,7 @@ namespace dlib ...@@ -147,7 +150,7 @@ namespace dlib
} }
// normalize the state // normalize the state
state = state/conj(trans(state))*state; state = state/(std::sqrt(sum(norm(state))));
return value; return value;
} }
...@@ -193,7 +196,7 @@ namespace dlib ...@@ -193,7 +196,7 @@ namespace dlib
} }
// normalize the state // normalize the state
temp.state = temp.state/conj(trans(temp.state))*temp.state; temp.state = temp.state/std::sqrt(sum(norm(temp.state)));
temp.swap(*this); temp.swap(*this);
......
...@@ -180,12 +180,17 @@ namespace dlib ...@@ -180,12 +180,17 @@ namespace dlib
{ {
/*! /*!
REQUIREMENTS ON T REQUIREMENTS ON T
T must be some object that implements an interface compatible with T must be some object that inherits from gate_exp and implements its own
a gate_exp or gate object. version of operator() and compute_state_element().
WHAT THIS OBJECT REPRESENTS WHAT THIS OBJECT REPRESENTS
This object represents an expression that evaluates to a quantum gate This object represents an expression that evaluates to a quantum gate
that operates on T::num_bits qubits. that operates on T::num_bits qubits.
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.
!*/ !*/
public: public:
...@@ -221,8 +226,8 @@ namespace dlib ...@@ -221,8 +226,8 @@ namespace dlib
- reg.num_bits() == num_bits - reg.num_bits() == num_bits
ensures ensures
- applies this quantum gate to the given quantum register - applies this quantum gate to the given quantum register
- Let M represent the matrix for this quantum gate - Let M represent the matrix for this quantum gate, then
- #reg().state_vector() = M*reg().state_vector() #reg().state_vector() = M*reg().state_vector()
!*/ !*/
template <typename exp> template <typename exp>
...@@ -236,8 +241,8 @@ namespace dlib ...@@ -236,8 +241,8 @@ namespace dlib
- reg.nc() == 1 - reg.nc() == 1
- 0 <= row_idx < dims - 0 <= row_idx < dims
ensures ensures
- Let M represent the matrix for this gate. - Let M represent the matrix for this gate, then
- returns rowm(M*reg, row_idx) this function returns rowm(M*reg, row_idx)
(i.e. returns the row_idx row of what you get when you apply this (i.e. returns the row_idx row of what you get when you apply this
gate to the given column vector in reg) gate to the given column vector in reg)
- This function works by calling ref().compute_state_element(reg,row_idx) - This function works by calling ref().compute_state_element(reg,row_idx)
...@@ -263,7 +268,7 @@ namespace dlib ...@@ -263,7 +268,7 @@ namespace dlib
WHAT THIS OBJECT REPRESENTS WHAT THIS OBJECT REPRESENTS
This object represents a quantum gate that is the tensor product of This object represents a quantum gate that is the tensor product of
two quantum gates. two other quantum gates.
As an example, suppose you have 3 registers, reg_high, reg_low, and reg_all. Also As an example, suppose you have 3 registers, reg_high, reg_low, and reg_all. Also
...@@ -306,8 +311,8 @@ namespace dlib ...@@ -306,8 +311,8 @@ namespace dlib
- 0 <= r < dims - 0 <= r < dims
- 0 <= c < dims - 0 <= c < dims
ensures ensures
- Let M denote the tensor product of lhs with rhs - Let M denote the tensor product of lhs with rhs, then this function
- returns M(r,c) returns M(r,c)
(i.e. returns lhs(r/U::dims,c/U::dims)*rhs(r%U::dims, c%U::dims)) (i.e. returns lhs(r/U::dims,c/U::dims)*rhs(r%U::dims, c%U::dims))
!*/ !*/
...@@ -322,8 +327,8 @@ namespace dlib ...@@ -322,8 +327,8 @@ namespace dlib
- reg.nc() == 1 - reg.nc() == 1
- 0 <= row_idx < dims - 0 <= row_idx < dims
ensures ensures
- Let M represent the matrix for this gate. - Let M represent the matrix for this gate, then this function
- returns rowm(M*reg, row_idx) returns rowm(M*reg, row_idx)
(i.e. returns the row_idx row of what you get when you apply this (i.e. returns the row_idx row of what you get when you apply this
gate to the given column vector in reg) gate to the given column vector in reg)
- This function works by calling rhs.compute_state_element() and using elements - This function works by calling rhs.compute_state_element() and using elements
...@@ -395,8 +400,8 @@ namespace dlib ...@@ -395,8 +400,8 @@ namespace dlib
- 0 <= r < dims - 0 <= r < dims
- 0 <= c < dims - 0 <= c < dims
ensures ensures
- Let M denote the matrix for this gate - Let M denote the matrix for this gate, then this function
- returns a const reference to M(r,c) returns a const reference to M(r,c)
!*/ !*/
qc_scalar_type& operator() ( qc_scalar_type& operator() (
...@@ -408,8 +413,8 @@ namespace dlib ...@@ -408,8 +413,8 @@ namespace dlib
- 0 <= r < dims - 0 <= r < dims
- 0 <= c < dims - 0 <= c < dims
ensures ensures
- Let M denote the matrix for this gate - Let M denote the matrix for this gate, then this function
- returns a non-const reference to M(r,c) returns a non-const reference to M(r,c)
!*/ !*/
template <typename exp> template <typename exp>
...@@ -423,8 +428,8 @@ namespace dlib ...@@ -423,8 +428,8 @@ namespace dlib
- reg.nc() == 1 - reg.nc() == 1
- 0 <= row_idx < dims - 0 <= row_idx < dims
ensures ensures
- Let M represent the matrix for this gate. - Let M represent the matrix for this gate, then this function
- returns rowm(M*reg, row_idx) returns rowm(M*reg, row_idx)
(i.e. returns the row_idx row of what you get when you apply this (i.e. returns the row_idx row of what you get when you apply this
gate to the given column vector in reg) gate to the given column vector in reg)
!*/ !*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment