【发布时间】:2016-04-17 19:33:16
【问题描述】:
我正在研究一种在课堂上使用 tooffoli 门的遗传算法。我已经让遗传算法工作了,但是速度很慢。
评估函数对每个“有机体”运行约 10,000 次 tooffoli 门函数。人口为 1,000 人,每代运行超过 100,000 次。这是迄今为止我的遗传算法中最慢的部分。
对于我的实现,Toffoli Gate 作用于位字符串。每个位的状态为 None、On、Off 或 Flip。每个字符串只有一位可以具有 FLIP 状态。如果状态为 ON 的所有位均为 1,且所有设置为 OFF 的位均为 0,则 toffoli 门将设置为 FLIP 的位翻转,忽略 None 位。
举例
X = FLIP
@ = ON
O = OFF
- = NONE
那么“1001”的输入和“X0-@”的toffoli门应该是这样的
1001
XO-@
----
0001
什么是实现这个的快速方法?
我的初始实现使用位集。
#define BITLEN 10
#define INPUT std::bitset<BITLEN>
#define GATE std::bitset<BITLEN*2>
void toffoli(const GATE* gate, INPUT* state) {
bool all_conditions = true;
int flip_index = -1;
bool set = false;
for (int i = 0; i < BITLEN; i++) {
/*a0 and a1 represent the state of A
11 is FLIP, 10 is ON, 01 is OFF, and 00 is NONE */
bool A = (*state)[i];
bool a1 = (*gate)[i*2];
bool a0 = (*gate)[(i*2)+1];
if (a1&&a0) {
flip_index = i;
set = true;
}
//NONE or FLIP have no condition to meet
//ON means A must be true
//OFF means A must be false
//this simplifies to the below if statement
if (!((!a0&&!a1) || (!A&&a1) || (A&&a0))) {
all_conditions = false;
break;
}
}
//if all conditions are met flip the bit with state FLIP
//check set flag in case gate is not valid
if (all_conditions && set)
state->flip(flip_index);
}
【问题讨论】:
标签: c++ algorithm performance