【问题标题】:Fast Toffoli Gate Implementation快速 Tooffoli Gate 实施
【发布时间】: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


    【解决方案1】:

    改变你的门代表:

    struct GATE {
        std::bitset<BITLEN> required_on;
        std::bitset<BITLEN> required_off;
        std::bitset<BITLEN> flip;
    };
    

    然后你可以通过位操作非常高效地实现操作:

    void toffoli(const GATE* gate, INPUT* state) {
        if((*state & (gate->required_on | gate->required_off)) == gate->required_on)
            *state ^= gate->flip;
        }
    }
    

    【讨论】:

    • 使用 gcc 版本 4.8.4 和 c++11 编译会出现错误,提示未为 bitset 定义位运算符。
    • 另外,*state ^= flip; 应该是 *state ^= gate-&gt;flip;
    • 经过一番研究,似乎需要在按位 & 周围加上一组额外的括号,否则编译器会混淆 & 应该绑定到什么。
    • 已更正,我也将其更改为仅使用一个比较,而不是两个。
    猜你喜欢
    • 2021-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多