Skip to content

Commit 85ed71f

Browse files
committed
Add Bell state example for QClojure
1 parent ab39960 commit 85ed71f

3 files changed

Lines changed: 197 additions & 1 deletion

File tree

deps.edn

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@
3939
org.lwjgl/lwjgl-stb$natives-windows {:mvn/version "3.3.6"}
4040
org.lwjgl/lwjgl-stb$natives-macos {:mvn/version "3.3.6"}
4141
generateme/fastmath {:mvn/version "3.0.0-alpha3"}
42-
clj-http/clj-http {:mvn/version "3.13.1"}}
42+
clj-http/clj-http {:mvn/version "3.13.1"}
43+
44+
org.soulspace/qclojure {:mvn/version "0.21.0"}
45+
}
4346

4447
:aliases
4548
{;; Build the site with `clojure -M:clay -A:markdown`

site/db.edn

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@
124124
:affiliation [:cim]}
125125
{:id :luke-zeitlin
126126
:name "Luke Zeitlin"
127+
:affiliation []}
128+
{:id :ludgersolbach
129+
:name "Ludger Solbach"
130+
:url "https://github.com/lsolbach"
131+
:links [{:icon "github" :href "https://github.com/lsolbach"}]
127132
:affiliation []}]
128133

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

0 commit comments

Comments
 (0)