| 1 | <h2>Full example</h2> |
| 2 | <pre class="language-cpp"><code>// https://github.com/Qiskit/openqasm |
| 3 | /* |
| 4 | * Repeat-until-success circuit for Rz(theta), |
| 5 | * cos(theta-pi)=3/5, from Nielsen and Chuang, Chapter 4. |
| 6 | */ |
| 7 | OPENQASM 3; |
| 8 | include "stdgates.inc"; |
| 9 | |
| 10 | /* |
| 11 | * Applies identity if out is 01, 10, or 11 and a Z-rotation by |
| 12 | * theta + pi where cos(theta)=3/5 if out is 00. |
| 13 | * The 00 outcome occurs with probability 5/8. |
| 14 | */ |
| 15 | def segment qubit[2]:anc, qubit:psi -> bit[2] { |
| 16 | bit[2] b; |
| 17 | reset anc; |
| 18 | h anc; |
| 19 | ccx anc[0], anc[1], psi; |
| 20 | s psi; |
| 21 | ccx anc[0], anc[1], psi; |
| 22 | z psi; |
| 23 | h anc; |
| 24 | measure anc -> b; |
| 25 | return b; |
| 26 | } |
| 27 | |
| 28 | qubit input; |
| 29 | qubit ancilla[2]; |
| 30 | bit flags[2] = "11"; |
| 31 | bit output; |
| 32 | |
| 33 | reset input; |
| 34 | h input; |
| 35 | |
| 36 | // braces are optional in this case |
| 37 | while(int(flags) != 0) { |
| 38 | flags = segment ancilla, input; |
| 39 | } |
| 40 | rz(pi - arccos(3 / 5)) input; |
| 41 | h input; |
| 42 | output = measure input; // should get zero |
| 43 | </code></pre> |