Skip to content

Commit d37a1a6

Browse files
Merge pull request #99 from lsolbach/main
updated qclojure and fixed formatting
2 parents 7fcef6f + 661f817 commit d37a1a6

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

deps.edn

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444

4545
com.github.psambit9791/jdsp {:mvn/version "3.1.0"}
4646

47-
org.soulspace/qclojure {:mvn/version "0.21.0"}
47+
org.soulspace/qclojure {:mvn/version "0.22.0"}
4848
}
4949

5050
:aliases

src/qclojure/examples/bell_state_circuit.clj

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
;; are reversible.
6060
;;
6161
;; Common quantum gates include:
62+
;;
6263
;; - **Hadamard Gate (H)**: Creates superposition by transforming a qubit from a definite state (|0⟩ or |1⟩)
6364
;; into an equal superposition of both states.
6465
;; - **Pauli-X Gate (X)**: Also known as the quantum NOT gate, it flips the state of a qubit (|0⟩ to |1⟩ and vice versa).
@@ -68,6 +69,7 @@
6869
;; qubit is in the state |1⟩. It is essential for creating entanglement between qubits.
6970
;;
7071
;; ## What is a Bell State?
72+
;;
7173
;; A Bell state is a specific quantum state of two qubits that represents the simplest and most
7274
;; well-known example of quantum entanglement. The Bell states are maximally entangled states
7375
;; and are used in various quantum information protocols, including quantum teleportation and
@@ -85,8 +87,10 @@
8587
;; which means that other combinations like |01⟩ or |10⟩ will never be observed in this state.
8688
;;
8789
;; ## Creating the Bell State Circuit
90+
;;
8891
;; The following code creates a simple quantum circuit that generates a Bell state.
8992
;; First, we need to require the necessary namespaces from QClojure.
93+
9094
(require '[org.soulspace.qclojure.domain.state :as state]
9195
'[org.soulspace.qclojure.domain.circuit :as circuit]
9296
'[org.soulspace.qclojure.application.visualization :as viz]
@@ -103,22 +107,28 @@
103107
(circuit/cnot-gate 0 1)))
104108

105109
;; We can visualize the circuit as ASCII art for the REPL.
110+
106111
^kind/code
107112
(viz/visualize-circuit :ascii bell-state-circuit)
108113

109114
;; For notebooks and documents, we can also visualize the circuit as SVG.
115+
110116
^kind/hiccup
111117
(viz/visualize-circuit :svg bell-state-circuit)
112118

113119
;; ## Executing the Bell State Circuit
120+
;;
114121
;; To quickly test the circuit in the REPL, we can use the execute-circuit function
115122
;; from the circuit namespace.
123+
116124
(def result (circuit/execute-circuit bell-state-circuit))
117125

118126
;; The result is a map that contains the final state of the qubits after executing the circuit.
127+
119128
result
120129

121130
;; ## Using Simulators to Execute the Circuit
131+
;;
122132
;; We can also use a quantum backend to execute the circuit with more options.
123133
;; QClojure provides two different simulator backends: an ideal simulator backend
124134
;; and a hardware simulator backend.
@@ -127,44 +137,53 @@ result
127137
;; that are present in real quantum hardware.
128138
;;
129139
;; First, we need to require the necessary namespaces for the simulators.
140+
130141
(require
131142
'[org.soulspace.qclojure.application.backend :as backend]
132143
'[org.soulspace.qclojure.adapter.backend.ideal-simulator :as ideal-sim]
133144
'[org.soulspace.qclojure.adapter.backend.hardware-simulator :as hw-sim])
134145

135146
;; Let's first use the ideal simulator to execute the Bell state circuit.
147+
136148
(def ideal-simulator (ideal-sim/create-simulator))
137149

138150
;; We define some options for the execution, such as the results we want to obtain.
139151
;; In this case, we want to measure the qubits 10000 times, which is called the
140152
;; number of shots.
153+
141154
(def options {:result-specs {:measurements {:shots 10000}}})
142155

143156
;; Now we can execute the circuit using the ideal simulator and the defined options.
157+
144158
(def ideal-result
145159
(backend/execute-circuit ideal-simulator bell-state-circuit options))
146160

147161
;; We can visualize the frequencies of the measurements obtained from the
148162
;; ideal simulator as a histogram.
163+
149164
^kind/hiccup
150165
(viz/visualize-measurement-histogram :svg (get-in ideal-result [:results :measurement-results :frequencies]))
151166

152167
;; Now we use the hardware simulator to execute the Bell state circuit.
153168
;; The hardware simulator simulates the quantum circuit with noise and errors
154169
;; that are present in real quantum hardware.
170+
155171
(def hardware-simulator (hw-sim/create-hardware-simulator))
156172

157173
;; We can also select a specific quantum device to simulate. We choose the
158174
;; IBM Lagos quantum device for this example. The IBM Lagos is a 7-qubit quantum
159175
;; computer that is available on the IBM Quantum platform.
176+
160177
(backend/select-device hardware-simulator :ibm-lagos)
161178

162179
;; We execute the circuit using the hardware simulator and the defined options.
180+
163181
(def hardware-result
164182
(backend/execute-circuit hardware-simulator bell-state-circuit options))
165183

166184
;; We can visualize the result of the hardware simulation as a histogram of the
167185
;; measurement frequencies to compare it with the ideal simulation result.
186+
168187
^kind/hiccup
169188
(viz/visualize-measurement-histogram :svg (get-in hardware-result [:results :measurement-results :frequencies]))
170189

@@ -177,6 +196,7 @@ result
177196
;; We will explore more complex circuits in future examples.
178197
;;
179198
;; ## Conclusion
199+
;;
180200
;; In this example, we created a simple quantum circuit that generates a Bell state,
181201
;; visualized the circuit, and executed it using both an ideal simulator and a hardware
182202
;; simulator provided by QClojure. We also visualized the measurement results as histograms

0 commit comments

Comments
 (0)