|
| 1 | +^{:kindly/hide-code true |
| 2 | + :clay {:title "Bell State Circuit" |
| 3 | + :quarto {:author :ludgersolbach |
| 4 | + :draft true |
| 5 | + :type :post |
| 6 | + :date "2025-10-10"}}} |
| 7 | +(ns qclojure.examples.bell-state-circuit |
| 8 | + (:require |
| 9 | + [scicloj.kindly.v4.kind :as kind])) |
| 10 | + |
| 11 | +;; # Bell State Circuit Example |
| 12 | +;; This example demonstrates how to create and visualize a Bell state circuit and how to simulate |
| 13 | +;; a quantum computer executing the circuit, using both an ideal simulator and a hardware simulator, |
| 14 | +;; on classical hardware using QClojure. |
| 15 | +;; |
| 16 | +;; ## What is QClojure? |
| 17 | +;; QClojure is a Clojure library for quantum computing that provides tools to create, |
| 18 | +;; execute, and visualize quantum circuits. It allows users to define quantum circuits using a |
| 19 | +;; high-level, functional programming approach. |
| 20 | +;; QClojure supports various quantum gates, measurements, and state manipulations, making it |
| 21 | +;; suitable for building quantum algorithms and exploring quantum computing concepts. |
| 22 | +;; It comes with a variety of quantum algorithms and provides simulators to run quantum circuits |
| 23 | +;; and algorithms on classical hardware. |
| 24 | +;; |
| 25 | +;; With extensions (e.g. for Amazon Braket), QClojure enables users to run their quantum |
| 26 | +;; circuits on real quantum hardware. |
| 27 | +;; |
| 28 | +;; To use QClojure, you have to include it as a dependency in your Clojure |
| 29 | +;; project. Please use the [latest version](https://clojars.org/org.soulspace/qclojure). |
| 30 | +;; |
| 31 | +;; ## What is a Quantum Computer? |
| 32 | +;; A quantum computer is a type of computing device that leverages the principles of quantum mechanics |
| 33 | +;; to perform computations. Unlike classical computers, which use bits as the basic unit of information |
| 34 | +;; (0s and 1s), quantum computers use quantum bits or qubits. Qubits can exist in multiple states simultaneously |
| 35 | +;; due to a property called superposition. This allows quantum computers to process a vast number of |
| 36 | +;; possibilities at once. |
| 37 | +;; Another key property of quantum computers is entanglement, where the state of one qubit can be |
| 38 | +;; directly related to the state of another, regardless of the distance between them. This phenomenon |
| 39 | +;; enables quantum computers to perform certain calculations much more efficiently than classical computers. |
| 40 | +;; |
| 41 | +;; ## What is a Quantum Circuit? |
| 42 | +;; A quantum circuit is a model for quantum computation in which a computation is represented as a |
| 43 | +;; sequence of quantum gates, which are the quantum analogs of classical logic gates. Quantum circuits |
| 44 | +;; manipulate qubits through these gates to perform operations and transformations on their quantum states. |
| 45 | +;; Quantum circuits are typically visualized using circuit diagrams, where qubits are represented as |
| 46 | +;; horizontal lines and quantum gates as symbols placed along these lines. |
| 47 | +;; Quantum circuits can be used to implement quantum algorithms, which are designed to solve specific |
| 48 | +;; problems more efficiently than classical algorithms. Examples of quantum algorithms include Shor's |
| 49 | +;; algorithm for factoring large numbers and Grover's algorithm for searching unsorted databases. |
| 50 | +;; |
| 51 | +;; ## What is a Quantum Gate? |
| 52 | +;; A quantum gate is a fundamental building block of quantum circuits, analogous to classical logic gates |
| 53 | +;; used in classical computing. Quantum gates manipulate the state of qubits, which are the basic |
| 54 | +;; units of quantum information. Unlike classical bits that can be either 0 or 1, |
| 55 | +;; qubits can exist in a superposition of states, allowing quantum gates to perform complex operations. |
| 56 | +;; Quantum gates are represented as unitary matrices, which ensure that the operations they perform |
| 57 | +;; are reversible. |
| 58 | +;; |
| 59 | +;; Common quantum gates include: |
| 60 | +;; - **Hadamard Gate (H)**: Creates superposition by transforming a qubit from a definite state (|0⟩ or |1⟩) |
| 61 | +;; into an equal superposition of both states. |
| 62 | +;; - **Pauli-X Gate (X)**: Also known as the quantum NOT gate, it flips the state of a qubit (|0⟩ to |1⟩ and vice versa). |
| 63 | +;; - **Pauli-Y Gate (Y)**: Similar to the X gate but also introducess a phase shift. |
| 64 | +;; - **Pauli-Z Gate (Z)**: Introduces a phase flip to the |1⟩ state while leaving the |0⟩ state unchanged. |
| 65 | +;; - **CNOT Gate (Controlled NOT)**: A two-qubit gate that flips the state of the target qubit if the control |
| 66 | +;; qubit is in the state |1⟩. It is essential for creating entanglement between qubits. |
| 67 | +;; |
| 68 | +;; ## What is a Bell State? |
| 69 | +;; A Bell state is a specific quantum state of two qubits that represents the simplest and most |
| 70 | +;; well-known example of quantum entanglement. The Bell states are maximally entangled states |
| 71 | +;; and are used in various quantum information protocols, including quantum teleportation and |
| 72 | +;; superdense coding. |
| 73 | +;; Bell states are fundamental in the study of quantum mechanics and quantum computing, |
| 74 | +;; illustrating the non-classical correlations that can exist between quantum systems. |
| 75 | +;; |
| 76 | +;; There are four different Bell states, but the most commonly referenced one is: |
| 77 | +;; |Φ+⟩ = (|00⟩ + |11⟩) / √2 |
| 78 | +;; |
| 79 | +;; This state indicates that if one qubit is measured to be in the state |0⟩, the other qubit will |
| 80 | +;; also be in the state |0⟩, and similarly for the state |1⟩, demonstrating perfect correlation between |
| 81 | +;; the two qubits. |
| 82 | +;; The probability of measuring either |00⟩ or |11⟩ is equal, each with a probability of 0.5, |
| 83 | +;; which means that other combinations like |01⟩ or |10⟩ will never be observed in this state. |
| 84 | +;; |
| 85 | +;; ## Creating the Bell State Circuit |
| 86 | +;; The following code creates a simple quantum circuit that generates a Bell state. |
| 87 | +;; First, we need to require the necessary namespaces from QClojure. |
| 88 | +(require '[org.soulspace.qclojure.domain.state :as state] |
| 89 | + '[org.soulspace.qclojure.domain.circuit :as circuit] |
| 90 | + '[org.soulspace.qclojure.application.visualization :as viz] |
| 91 | + '[org.soulspace.qclojure.adapter.visualization.ascii :as ascii] |
| 92 | + '[org.soulspace.qclojure.adapter.visualization.svg :as svg]) |
| 93 | + |
| 94 | +;; Next, we create a quantum circuit with two qubits and apply the necessary quantum gates |
| 95 | +;; to generate the Bell state. We use the Hadamard gate (H) on the first qubit to create |
| 96 | +;; superposition, followed by a CNOT gate to entangle the two qubits. |
| 97 | + |
| 98 | +(def bell-state-circuit |
| 99 | + (-> (circuit/create-circuit 2 "Bell State Circuit" "Creates a Bell state.") |
| 100 | + (circuit/h-gate 0) |
| 101 | + (circuit/cnot-gate 0 1))) |
| 102 | + |
| 103 | +;; We can visualize the circuit as ASCII art for the REPL. |
| 104 | +^kind/code |
| 105 | +(viz/visualize-circuit :ascii bell-state-circuit) |
| 106 | + |
| 107 | +;; For notebooks and documents, we can also visualize the circuit as SVG. |
| 108 | +^kind/hiccup |
| 109 | +(viz/visualize-circuit :svg bell-state-circuit) |
| 110 | + |
| 111 | +;; ## Executing the Bell State Circuit |
| 112 | +;; To quickly test the circuit in the REPL, we can use the execute-circuit function |
| 113 | +;; from the circuit namespace. |
| 114 | +(def result (circuit/execute-circuit bell-state-circuit)) |
| 115 | + |
| 116 | +;; The result is a map that contains the final state of the qubits after executing the circuit. |
| 117 | +result |
| 118 | + |
| 119 | +;; ## Using Simulators to Execute the Circuit |
| 120 | +;; We can also use a quantum backend to execute the circuit with more options. |
| 121 | +;; QClojure provides two different simulator backends: an ideal simulator backend |
| 122 | +;; and a hardware simulator backend. |
| 123 | +;; The ideal simulator simulates the quantum circuit without any noise or errors, |
| 124 | +;; while the hardware simulator simulates the quantum circuit with noise and errors |
| 125 | +;; that are present in real quantum hardware. |
| 126 | +;; |
| 127 | +;; First, we need to require the necessary namespaces for the simulators. |
| 128 | +(require |
| 129 | + '[org.soulspace.qclojure.application.backend :as backend] |
| 130 | + '[org.soulspace.qclojure.adapter.backend.ideal-simulator :as ideal-sim] |
| 131 | + '[org.soulspace.qclojure.adapter.backend.hardware-simulator :as hw-sim]) |
| 132 | + |
| 133 | +;; Let's first use the ideal simulator to execute the Bell state circuit. |
| 134 | +(def ideal-simulator (ideal-sim/create-simulator)) |
| 135 | + |
| 136 | +;; We define some options for the execution, such as the results we want to obtain. |
| 137 | +;; In this case, we want to measure the qubits 100 times (shots). |
| 138 | +(def options {:result-specs {:measurements {:shots 100}}}) |
| 139 | + |
| 140 | +;; Now we can execute the circuit using the ideal simulator and the defined options. |
| 141 | +(def ideal-result |
| 142 | + (backend/execute-circuit ideal-simulator bell-state-circuit options)) |
| 143 | + |
| 144 | +;; The result is a map that contains the measurement results and other information |
| 145 | +;; about the execution. |
| 146 | +ideal-result |
| 147 | + |
| 148 | +;; We can visualize the frequencies of the measurements obtained from the |
| 149 | +;; ideal simulator as a histogram. |
| 150 | +^kind/hiccup |
| 151 | +(viz/visualize-measurement-histogram :svg (get-in ideal-result [:results :measurement-results :frequencies])) |
| 152 | + |
| 153 | +;; Now we you the hardware simulator to execute the Bell state circuit. |
| 154 | +;; The hardware simulator simulates the quantum circuit with noise and errors |
| 155 | +;; that are present in real quantum hardware. |
| 156 | +(def hardware-simulator (hw-sim/create-hardware-simulator)) |
| 157 | + |
| 158 | +;; We can also select a specific quantum device to simulate. We choose the |
| 159 | +;; IBM Lagos quantum device for this example. The IBM Lagos is a 7-qubit quantum |
| 160 | +;; computer that is available on the IBM Quantum Experience platform. |
| 161 | +(backend/select-device hardware-simulator :ibm-lagos) |
| 162 | + |
| 163 | +;; We execute the circuit using the hardware simulator and the defined options. |
| 164 | +(def hardware-result |
| 165 | + (backend/execute-circuit hardware-simulator bell-state-circuit options)) |
| 166 | + |
| 167 | +;; Here is the result of the hardware simulation. |
| 168 | +hardware-result |
| 169 | + |
| 170 | +;; We can visualize the result of the hardware simulation as a histogram of the |
| 171 | +;; measurement frequencies to compare it with the ideal simulation result. |
| 172 | +^kind/hiccup |
| 173 | +(viz/visualize-measurement-histogram :svg (get-in hardware-result [:results :measurement-results :frequencies])) |
| 174 | + |
| 175 | +;; We results are probabilistic, so we may not get exactly the same results every time we |
| 176 | +;; execute the circuit. However, we should see that the results from the ideal simulator |
| 177 | +;; are closer to the expected Bell state results (|00⟩ and |11⟩ with similar counts) compared to the |
| 178 | +;; hardware simulator, which may show some deviations due to noise and errors. |
| 179 | +;; This demonstrates the impact of quantum noise and errors on the execution of quantum circuits |
| 180 | +;; on real quantum hardware. |
| 181 | +;; |
| 182 | +;; ## Conclusion |
| 183 | +;; In this example, we created a simple quantum circuit that generates a Bell state, |
| 184 | +;; visualized the circuit, and executed it using both an ideal simulator and a hardware |
| 185 | +;; simulator provided by QClojure. We observed the differences in the measurement results |
| 186 | +;; between the two simulators, highlighting the effects of noise and errors in quantum computing. |
0 commit comments