本文整理汇总了C++中Complex::Set方法的典型用法代码示例。如果您正苦于以下问题:C++ Complex::Set方法的具体用法?C++ Complex::Set怎么用?C++ Complex::Set使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Complex
的用法示例。
在下文中一共展示了Complex::Set方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1:
Complex Complex::operator-()
{
///------------------------------
/// operator- (Negate Operator)
/// compute the negative of a
/// complex number
///------------------------------
Complex c;
c.Set(-R,-I);
return c;
}
示例2: main
//.........这里部分代码省略.........
//and r is the period we are trying to find to factor n. m is the
//value we measure from register one after the Fourier
//transformation.
double c, m;
//This is used to store the denominator of the fraction p / den where
//p / den is the best approximation to c with den <= q.
unsigned long long int den;
//This is used to store the numerator of the fraction p / den where
//p / den is the best approximation to c with den <= q.
unsigned long long int p;
//The integers e, a, and b are used in the end of the program when
//we attempts to calculate the factors of n given the period it
//measured.
//Factor is the factor that we find.
unsigned long long int e, a, b, factor;
//Shor's algorithm can sometimes fail, in which case you do it
//again. The done variable is set to 0 when the algorithm has
//failed. Only try a maximum number of tries.
unsigned int done = 0;
unsigned int tries = 0;
while (!done) {
if (tries >= 5) {
cout << "\tThere have been five failures, giving up." << endl << flush;
exit(0);
}
cout << "Step 5 starting attempt: " << tries+1 << endl << flush;
//Now populate register one in an even superposition of the
//integers 0 -> q - 1.
reg1->SetAverage(q - 1);
cout << "Step 5 complete." << endl << flush;
cout << "Step 6 starting attempt: " << tries+1 << endl << flush;
//Now we preform a modular exponentiation on the superposed
//elements of reg 1. That is, perform x^a mod n, but exploiting
//quantum parallelism a quantum computer could do this in one
//step, whereas we must calculate it once for each possible
//measurable value in register one. We store the result in a new
//register, reg2, which is entangled with the first register.
//This means that when one is measured, and collapses into a base
//state, the other register must collapse into a superposition of
//states consistent with the measured value in the other.. The
//size of the result modular exponentiation will be at most n, so
//the number of bits we will need is therefore less than or equal
//to log2 of n. At this point we also maintain a array of what
//each state produced when modularly exponised, this is because
//these registers would actually be entangled in a real quantum
//computer, this information is needed when collapsing the first
//register later.
//This counter variable is used to increase our probability amplitude.
tmp.Set(1,0);
//This for loop ranges over q, and puts the value of x^a mod n in
//modex[a]. It also increases the probability amplitude of the value
//of mdx[x^a mod n] in our array of complex probabilities.
for (unsigned long long int i = 0 ; i < q ; i++) {
//We must use this version of modexp instead of c++ builtins as
//they overflow when x^i is large.
tmpval = modexp(x,i,n);
modex[i] = tmpval;
mdx[tmpval] = mdx[tmpval] + tmp;
示例3: srand
//.........这里部分代码省略.........
mdx = new Complex[array_size];
// This is the second register. It needs to be big enough to hold
// the superposition of numbers ranging from 0 -> n - 1.
reg2 = new QuReg(RegSize(n));
cout << "Created register 2 of size " << RegSize(n) << endl << flush;
//Shor's algorithm can sometimes fail, in which case you do it
//again. The done variable is set to 0 when the algorithm has
//failed. Only try a maximum number of tries.
done = 0;
tries = 0;
//START TIME HERE!
startTime = get_clock();
} //Everything up to this point is pre processing done by thread 0.
// Wait for everyone to finish.
barrier(&global_barrier_counter1, num_threads, &global_barrier_mutex1,
&global_barrier_cond1);
while (!done) {
if (0 == thread_id) {
if (tries >= 5) {
cout << "There have been five failures, giving up." << endl << flush;
exit(0);
}
//Now populate register one in an even superposition of the
//integers 0 -> q - 1.
reg1->SetAverage(q - 1);
//Now we preform a modular exponentiation on the superposed
//elements of reg 1. That is, perform x^a mod n, but exploiting
//quantum parallelism a quantum computer could do this in one
//step, whereas we must calculate it once for each possible
//measurable value in register one. We store the result in a new
//register, reg2, which is entangled with the first register.
//This means that when one is measured, and collapses into a base
//state, the other register must collapse into a superposition of
//states consistent with the measured value in the other.. The
//size of the result modular exponentiation will be at most n, so
//the number of bits we will need is therefore less than or equal
//to log2 of n. At this point we also maintain a array of what
//each state produced when modularly exponised, this is because
//these registers would actually be entangled in a real quantum
//computer, this information is needed when collapsing the first
//register later.
//This counter variable is used to increase our probability amplitude.
tmp.Set(1,0);
}
// Wait for all threads.
barrier(&global_barrier_counter2, num_threads, &global_barrier_mutex2,
&global_barrier_cond2);
//This is the parallel version
for (int i = q_range_lower[thread_id] ; i <= q_range_upper[thread_id] ; i++) {
//We must use this version of modexp instead of c++ builtins as
//they overflow when x^i > 2^31.
tmpval = modexp(x,i,n);
modex[i] = tmpval;