首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

Quantum computing in action: IBM's Q experience and the quantum shell game(4)

Quantum computing in action: IBM's Q experience and the quantum shell game(4)

The mapRemember, we want to map our real-world problem onto the physical state of                the quantum processor, execute the logic, and reverse the mapping back to                reality.
In our shell game then, we need to:
  • Pass the number of the shell selected by our user to the backend.
  • Have the backend pass the data to the quantum processor in the form of                    QASM code that executes Grover’s algorithm.
  • Retrieve those results and parse the results.
  • Map the result data back into the reality (e.g. display which shell                    the user chose).
Phew! Hard to say, but not too hard to do. The QCode class does all the                work during instantiation. Here we break it down step-by-step.
                Step 1. Pass the number of the shells selected by the user to the                backendWe pass a parameter to the QCode constructor indicating which shell the                user chose. This guides the creation of the QASM code to properly reflect                the mapping of reality onto the quantum processor’s execution environment.                The QCode constructor code begins by setting up the first part of the QASM                code, which simply establishes five qubit and five classical                registers.
Listing 5. Set up the QASM                code
1
2
//5 qubits and 5 classical registers
string preCode = "include \"qelib1.inc\";qreg q[5];creg c[5];";




Why five? Truth be told, we only need two, but we’ll use five to keep this                demo in line with the Composer scores found in the  on the Q experience web site.
                Step 2. Have the backend pass the data to the quantum processor in the                form of QASM code that executes Grover’s algorithmWe assign states to the qubits. The system builds out the QASM based on                which state we are trying to represent. Remember, if the user selected                shell #1, we want to set the state of our qubits to the “00” state. Shell                #2 is state “01” and so forth. The switch statement handles setting up                these states. The code listing below shows how to assign these states:
Listing 6. Assign the                states
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
switch (coinUnderShellNumber)
            {

                case 1:  //#State=00
                    initCode = @"h q[1];
                                h q[2];
                                s q[1];
                                s q[2];
                                h q[2];
                                cx q[1],q[2];
                                h q[2];
                                s q[1];
                                s q[2];
                                ";
                    break;
                case 2:  //#State=01
                    initCode = @"h q[1];
                                h q[2];
                                s q[2];
                                h q[2];
                                cx q[1],q[2];
                                h q[2];
                                s q[2];";
                    break;
                case 3: //#State=10
                    initCode = @"h q[1];
                                h q[2];
                                s q[1];
                                h q[2];
                                cx q[1],q[2];
                                h q[2];
                                s q[1];";
                    break;
                case 4: //#State=11
                    initCode = @"h q[1];
                            h q[2];
                            h q[2];
                            cx q[1],q[2];
                            h q[2];
                            ";
                    break;
                default:
                    throw new Exception("There can only be 4 shells.");
            }




Finally, we add Grover’s algorithm to the end of this QASM code, as you can                see in the following code listing:
Listing 7. Add Grover's                    algorithm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//everything starting at the "double Hadamard"
            string postControlledNot = @"h q[1];
                                        h q[2];
                                        x q[1];
                                        x q[2];
                                        h q[2];
                                        cx q[1], q[2];
                                        h q[2];
                                        x q[1];
                                        x q[2];
                                        h q[1];
                                        h q[2];
                                        measure q[1] -> c[1];
                                        measure q[2] -> c[2];";




And we put it all together into a single string:
Listing 8. Put into a single                    string
1
this.qasm = preCode + initCode + postControlledNot;




And we set the number of shots to 500, so that our experiment will be run                enough times to wash out the probabilities.
Listing 9. Set the number of                    shots
1
this.shots = 500;

返回列表