Skip to content

Commit 3270cad

Browse files
committed
swap test added
1 parent 4cdfa86 commit 3270cad

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ Scheme for find the period `r` for `f(x) = 2^x mod 15`:
7474

7575
![Shor's Algorithm](./circuit_diagrams/07_shors_algorithm.png)
7676

77+
### Swap Test
78+
79+
> Task. For given two unknown quantum states, determine how much them differs.
80+
81+
![Swap test](./circuit_diagrams/c1_swap_test.png)
82+
7783
## References
7884

7985
- [Jonahtan Hui, Quantum Computing Series, Medium](https://medium.com/@jonathan_hui/qc-quantum-computing-series-10ddd7977abd)

c1_Swap_test.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'''
2+
Swap Test
3+
4+
Task. For given two unknown quantum states, determine how much them differs.
5+
6+
'''
7+
from qiskit import IBMQ, BasicAer
8+
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister, execute
9+
10+
qr = QuantumRegister(3) # Initialize qubits
11+
cr = ClassicalRegister(1) # Initialize bits for record measurements
12+
circuit = QuantumCircuit(qr, cr)
13+
14+
# Our 'unknown' states
15+
circuit.x(qr[1])
16+
17+
circuit.barrier()
18+
19+
# Swap test
20+
circuit.h(qr[2])
21+
circuit.cswap(qr[2], qr[0], qr[1])
22+
circuit.h(qr[2])
23+
24+
circuit.barrier()
25+
26+
# Measure
27+
circuit.measure(qr[2], cr[0])
28+
''' If unknown states are:
29+
* Orthogonal, then 0 is measured with probability 50%;
30+
* Equal, then 0 is measured with probability 100%.
31+
'''
32+
33+
# Run our circuit with local simulator
34+
backend = BasicAer.get_backend('qasm_simulator')
35+
shots = 1024
36+
results = execute(circuit, backend=backend, shots=shots).result()
37+
answer = results.get_counts()
38+
print(answer)
39+
# Since our states are orthogonal (qr[0] = |0>, qr[1] = |1>)
40+
# 0 is observed with probability approx. 50% and
41+
# 1 is observed with probability approx. 50%.

circuit_diagrams/c1_Swap_test.png

18.1 KB
Loading

0 commit comments

Comments
 (0)