From b58c05f5e2affb0a544022b25ea3fa3093248d90 Mon Sep 17 00:00:00 2001 From: Davis King Date: Sun, 30 Nov 2008 13:50:22 +0000 Subject: [PATCH] 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 --- dlib/quantum_computing/quantum_computing.h | 7 +++- .../quantum_computing_abstract.h | 39 +++++++++++-------- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/dlib/quantum_computing/quantum_computing.h b/dlib/quantum_computing/quantum_computing.h index b2f2fb774..3a933ca61 100644 --- a/dlib/quantum_computing/quantum_computing.h +++ b/dlib/quantum_computing/quantum_computing.h @@ -4,6 +4,7 @@ #define DLIB_QUANTUM_COMPUTINg_1_ #include +#include #include "../matrix.h" #include "../rand.h" #include "../enable_if.h" @@ -131,6 +132,8 @@ namespace dlib for (int i = 0; i < bit; ++i) 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) { const unsigned long field = r; @@ -147,7 +150,7 @@ namespace dlib } // normalize the state - state = state/conj(trans(state))*state; + state = state/(std::sqrt(sum(norm(state)))); return value; } @@ -193,7 +196,7 @@ namespace dlib } // 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); diff --git a/dlib/quantum_computing/quantum_computing_abstract.h b/dlib/quantum_computing/quantum_computing_abstract.h index 3288c9d77..26693c47e 100644 --- a/dlib/quantum_computing/quantum_computing_abstract.h +++ b/dlib/quantum_computing/quantum_computing_abstract.h @@ -180,12 +180,17 @@ namespace dlib { /*! REQUIREMENTS ON T - T must be some object that implements an interface compatible with - a gate_exp or gate object. + T must be some object that inherits from gate_exp and implements its own + version of operator() and compute_state_element(). WHAT THIS OBJECT REPRESENTS This object represents an expression that evaluates to a quantum gate 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: @@ -221,8 +226,8 @@ namespace dlib - reg.num_bits() == num_bits ensures - applies this quantum gate to the given quantum register - - Let M represent the matrix for this quantum gate - - #reg().state_vector() = M*reg().state_vector() + - Let M represent the matrix for this quantum gate, then + #reg().state_vector() = M*reg().state_vector() !*/ template @@ -236,8 +241,8 @@ namespace dlib - reg.nc() == 1 - 0 <= row_idx < dims ensures - - Let M represent the matrix for this gate. - - returns rowm(M*reg, row_idx) + - Let M represent the matrix for this gate, then + this function returns rowm(M*reg, row_idx) (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) @@ -263,7 +268,7 @@ namespace dlib WHAT THIS OBJECT REPRESENTS 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 @@ -306,8 +311,8 @@ namespace dlib - 0 <= r < dims - 0 <= c < dims ensures - - Let M denote the tensor product of lhs with rhs - - returns M(r,c) + - Let M denote the tensor product of lhs with rhs, then this function + returns M(r,c) (i.e. returns lhs(r/U::dims,c/U::dims)*rhs(r%U::dims, c%U::dims)) !*/ @@ -322,8 +327,8 @@ namespace dlib - reg.nc() == 1 - 0 <= row_idx < dims ensures - - Let M represent the matrix for this gate. - - returns rowm(M*reg, row_idx) + - Let M represent the matrix for this gate, then this function + returns rowm(M*reg, row_idx) (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 @@ -395,8 +400,8 @@ namespace dlib - 0 <= r < dims - 0 <= c < dims ensures - - Let M denote the matrix for this gate - - returns a const reference to M(r,c) + - Let M denote the matrix for this gate, then this function + returns a const reference to M(r,c) !*/ qc_scalar_type& operator() ( @@ -408,8 +413,8 @@ namespace dlib - 0 <= r < dims - 0 <= c < dims ensures - - Let M denote the matrix for this gate - - returns a non-const reference to M(r,c) + - Let M denote the matrix for this gate, then this function + returns a non-const reference to M(r,c) !*/ template @@ -423,8 +428,8 @@ namespace dlib - reg.nc() == 1 - 0 <= row_idx < dims ensures - - Let M represent the matrix for this gate. - - returns rowm(M*reg, row_idx) + - Let M represent the matrix for this gate, then this function + returns rowm(M*reg, row_idx) (i.e. returns the row_idx row of what you get when you apply this gate to the given column vector in reg) !*/