|
| 1 | +--- |
| 2 | +jupyter: |
| 3 | + jupytext: |
| 4 | + text_representation: |
| 5 | + extension: .md |
| 6 | + format_name: markdown |
| 7 | + format_version: '1.3' |
| 8 | + jupytext_version: 1.13.8 |
| 9 | + kernelspec: |
| 10 | + display_name: Python 3 (ipykernel) |
| 11 | + language: python |
| 12 | + name: python3 |
| 13 | +--- |
| 14 | + |
| 15 | +# Compiling and simulating a 10-qubit Quantum Fourier Transform (QFT) algorithm |
| 16 | + |
| 17 | +In this notebook, we simulate a 10-qubit Quantum Fourier Transform (QFT) algorithm. |
| 18 | +The QFT algorithm is one of the most important quantum algorithms in quantum computing. |
| 19 | +It is, for instance, part of the Shor algorithm for integer factorization. |
| 20 | +The following code defines a 10-qubit QFT algorithm using CNOT and single qubit rotations and runs the simulation both at the gate level and at the pulse level. |
| 21 | + |
| 22 | +```python |
| 23 | +from qutip import basis, fidelity |
| 24 | +from qutip_qip.algorithms import qft_gate_sequence |
| 25 | +from qutip_qip.device import LinearSpinChain |
| 26 | +from qutip.ipynbtools import version_table |
| 27 | +import qutip_qip |
| 28 | + |
| 29 | +num_qubits = 10 |
| 30 | +# The QFT circuit |
| 31 | +qc = qft_gate_sequence(num_qubits, swapping=False, to_cnot=True) |
| 32 | +# Gate-level simulation |
| 33 | +state1 = qc.run(basis([2] * num_qubits, [0] * num_qubits)) |
| 34 | +# Pulse-level simulation |
| 35 | +processor = LinearSpinChain(num_qubits) |
| 36 | +processor.load_circuit(qc) |
| 37 | +state2 = processor.run_state(basis([2] * num_qubits, |
| 38 | + [0] * num_qubits)).states[-1] |
| 39 | +fidelity(state1, state2) |
| 40 | +``` |
| 41 | + |
| 42 | +We plot the compiled pulses in the cell below. |
| 43 | +The pulses plotted implement the QFT algorithm represented in the native gates of the spin chain model, with single-qubit gates marked by rotations over the $x$- and $z$-axes and the iSWAP gate implemented through the spin-spin exchange interaction, marked by $g_i$. |
| 44 | +While the sign for single-qubit drive denotes the phase of the control pulse, the negative sign in the coupling strengths $g_i$ is only a result of the convention used in the definition of the interaction, defined in \cref{eq:ham spin chain}. |
| 45 | + |
| 46 | +```python |
| 47 | +def get_control_latex(model): |
| 48 | + """ |
| 49 | + Get the labels for each Hamiltonian. |
| 50 | + It is used in the method method :meth:`.Processor.plot_pulses`. |
| 51 | + It is a 2-d nested list, in the plot, |
| 52 | + a different color will be used for each sublist. |
| 53 | + """ |
| 54 | + num_qubits = model.num_qubits |
| 55 | + num_coupling = model._get_num_coupling() |
| 56 | + return [ |
| 57 | + {f"sx{m}": r"$\sigma_x^{}$".format(m) for m in range(num_qubits)}, |
| 58 | + {f"sz{m}": r"$\sigma_z^{}$".format(m) for m in range(num_qubits)}, |
| 59 | + {f"g{m}": r"$g_{}$".format(m) for m in range(num_coupling)}, |
| 60 | + ] |
| 61 | + |
| 62 | + |
| 63 | +fig, axes = processor.plot_pulses( |
| 64 | + figsize=(5, 7), dpi=150, pulse_labels=get_control_latex(processor.model) |
| 65 | +) |
| 66 | +axes[-1].set_xlabel("$t$"); |
| 67 | +``` |
| 68 | + |
| 69 | +```python |
| 70 | +print("qutip-qip version:", qutip_qip.version.version) |
| 71 | +version_table() |
| 72 | +``` |
0 commit comments