inline double Grape::Phi4TraceCav() const{
	//the measure implemented, phi (PHI_4_sub) is phi = |sum_{i=0,1} <i|U_desired* UN-1....U_0|i>|^2/2^2
	std::complex<double> temp1=0.0;
	matrix<std::complex<double> > Rtemp=MOs::TraceOutB(rho_[num_time_-1],5);///5=dimCav
	for(size_t q=0; q< 2; ++q){
		for(size_t p=0; p<dimQ_; ++p){
			temp1 += std::conj(rho_desired_(p,q))*Rtemp(p,q);
		}
	}
	return std::real(temp1*std::conj(temp1))*0.25;
}
inline double RobustGrape::GradPhi3(const size_t point, const size_t j, const size_t k) const{
	//phi_3
	std::complex<double> temp1=0;
	matrix<std::complex<double> > Rtemp;
	Rtemp=H_controls_tdep_[point][k]*rho_[point][j];
	for(size_t q=0; q< dim_; ++q){
		for(size_t p=0; p<dim_; ++p){
			temp1 +=std::conj(lambda_[point][j](p,q))*Rtemp(p,q);	
		}
	}	
	return epsilon_*std::imag(temp1);
}
inline double RobustGrape::GradPhi0(const size_t point, const size_t j, const size_t k) const{
	//phi_0
	std::complex<double> temp1=0;
	matrix<std::complex<double> > Rtemp;
	Rtemp=QOs::SuperHami(H_controls_tdep_[point][k],rho_[point][j]);
	//Fill in the upper triangle
	for (size_t p=0; p< dim_; ++p){
		for(size_t q=p+1; q < dim_; ++q){
			Rtemp(p,q)=std::conj(Rtemp(q,p));
		}
	}	
	for(size_t q=0; q< dim_; ++q){
		for(size_t p=0; p<dim_; ++p){
			temp1 +=std::conj(lambda_[point][j](p,q))*Rtemp(p,q);	
		}
	}
	// std::cout << h_*QOs::Expectation(L,-i*H*R+i*R*H)- h_*temp1 << std::endl;
	// std::cout << temp1 << std::endl;
	// 	Rtemp.SetOutputStyle(Matrix);
	// std::cout << Rtemp << std::endl;		
	return epsilon_*real(temp1);
}
inline double RobustGrape::GradPhi4Sub2(const size_t point, const size_t j, const size_t k) const{
	//Phi_4_subsystem
	//2.0*h_*imag(QOs::Expectation( MOs::Dagger(L),H*R)*QOs::Expectation(MOs::Dagger(R),L));
	std::complex<double> temp1=0, temp2=0;
	matrix<std::complex<double> > Rtemp;
	Rtemp=H_controls_tdep_[point][k]*rho_[point][j];
	for(size_t q=0; q< 2; ++q){
		for(size_t p=0; p<dim_; ++p){
			temp1 +=std::conj(lambda_[point][j](p,q))*Rtemp(p,q);
			temp2 += std::conj(rho_[point][j](p,q))*lambda_[point][j](p,q);	
		}
	}
	return 2.0*epsilon_*std::imag(temp1*temp2);//*one_on_dim_*one_on_dim_;
}
inline double Grape::GradPhi4Sub2Filter(const size_t j, const size_t k) const{
	double gradient=0.0;
	if(j<nsubpixels_[k] || j>num_time_-nsubpixels_[k]) return gradient;
	std::complex<double> temp1=0, temp2=0;
	matrix<std::complex<double> > Rtemp;
	int endtime = j + 100;
	if(j>num_time_-100) endtime = num_time_;
	for(size_t l = (j>100)*(j-100); l <endtime; ++l){
	  Rtemp=cos(((double)l)*drive_freq_*h_ - (k%2)*M_PI/2)*H_controls_tdep_[k]*rho_[l]*exp(-abs((int)j-(int)l)*h_/filter_rate_)*(h_/filter_rate_/2.0 /filter_norm_);
	  for(size_t q=0; q< 2; ++q){
	  	 for(size_t p=0; p<dim_; ++p){
			temp1 +=std::conj(lambda_[l](p,q))*Rtemp(p,q);
			temp2 += std::conj(rho_[l](p,q))*lambda_[l](p,q);	
		 }
	  }
	  gradient +=2.0*epsilon_*std::imag(temp1*temp2);
	}
	return gradient;
}
inline double Grape::GradPhi4Sub2RWFilter(const size_t j, const size_t k) const{
	double gradient=0.0;
	double prefac;
	if(j<nsubpixels_[k]*12 || j>num_time_-12*nsubpixels_[k]) return gradient;
	int span=100;
	int endtime = j + span;
	if(j>num_time_-span) endtime = num_time_;
	
	double phase = config_*2*M_PI/nconfigs_; 
	
	for(size_t l = (j>span)*(j-span); l <endtime; ++l) 	
	//int l=j;
	{
		prefac = cos(phase + ((double)j)*(drive_freq_)*h_ - (k%2)*M_PI/2)*exp(-abs((int)j-(int)l)*h_/filter_rate_)*(h_/filter_rate_/2.0 /filter_norm_); 
		Udiag_[k][0][0]=Udiag_[k][1][0]=1;
		Udiag_[k][0][1]=exp(-std::complex<double>(0.0,phase +(double)j*drive_freq_*h_));
		Udiag_[k][0][2]=exp(-std::complex<double>(0.0,2.0*(phase + (double)j*drive_freq_*h_)));
		Udiag_[k][0][3]=exp(-std::complex<double>(0.0,3.0*(phase + (double)j*drive_freq_*h_)));
		Udiag_[k][1][1]=exp(std::complex<double>(0.0,(phase + (double)j*drive_freq_*h_)));
		Udiag_[k][1][2]=exp(std::complex<double>(0.0,2.0*(phase + (double)j*drive_freq_*h_)));	
		Udiag_[k][1][3]=exp(std::complex<double>(0.0,3.0*(phase + (double)j*drive_freq_*h_)));	
		for(size_t q=0; q< dim_; ++q){
			for(size_t p=0; p<dim_; ++p){
				H_controls_tdep_[k](p,q)= Udiag_[k][0][q]*H_controls_[k](p,q)*Udiag_[k][1][p]*prefac;
			
			}
		}		std::complex<double> temp1=0, temp2=0;
	matrix<std::complex<double> > Rtemp;
	Rtemp=H_controls_tdep_[k]*rho_[j];
	for(size_t q=0; q< 2; ++q){
		for(size_t p=0; p<dim_; ++p){
			temp1 +=std::conj(lambda_[j](p,q))*Rtemp(p,q);
			temp2 += std::conj(rho_[j](p,q))*lambda_[j](p,q);	
		}
	}
	
	gradient+= 2.0*epsilon_*std::imag(temp1*temp2);//*one_on_dim_*one_on_dim_;
	}
	return gradient;
}
예제 #7
0
void updateQR(Matrix & Q, Matrix & R, int NUM_TX, int NUM_RX,int i)
{
		
		int ni = NUM_RX - i;
		int ti = NUM_TX - i;
		Matrix Rtemp(ni,ti);
		Matrix Qtemp(ni,ni);
		Matrix Atemp(ni,ti);
		Matrix Itemp(ni,ni);
		int k,l;
		for(k = i; k < NUM_RX; k++)
		{
			for(l = i ; l < NUM_TX; l++)
			{
				Atemp(k-i , l-i) = R(k,l);
			}
		}
		for(k = 0; k < ni; k++)
		{
			for(l = 0; l < ni; l++)
			{
				if(k == l) Itemp(k,l) = 1;
				else Itemp(k,l) = 0;
			}
		}
		Matrix x(ni,1);
		for(k = 0; k < ni; k++)
			x(k,0) = Atemp(k,0);
		double xSize = eSize(x,ni);
		int sign; 
		if ( x(0,0) >= 0 ) sign = 1; 
		else sign = -1;
		Matrix g(ni,1);
		g(0,0) = x(0,0) + sign * xSize;
		for(k = 1; k < ni; k++)
			g(k,0) = x(k,0);
		double gSize = eSize(g,ni);
		double Qscale = 2/(gSize * gSize);
		Matrix ggT(ni,ni);
		ggT = g * (~g);
		for(k = 0; k < ni; k++)
			for(l = 0; l < ni; l++)
				ggT(k,l) = Qscale * ggT(k,l);
		Qtemp = Itemp - ggT;
		Rtemp = Qtemp * Atemp;

		Matrix Qeff(NUM_RX,NUM_RX);
		for(k = 0; k < NUM_RX; k++)
			for(l = 0; l < NUM_RX; l++)
				Qeff(k,l) = 0;
		for(k = 0; k < i; k++) Qeff(k,k) = 1;
		
		for(k = i; k < NUM_RX; k++)
		{
			for(l = i; l < NUM_TX; l++)
				R(k,l) = Rtemp(k-i,l-i);
			for(l = i; l < NUM_RX; l++)
				Qeff(k,l) = Qtemp(k-i,l-i);
		}
		Q = Q * Qeff; 
}