forked from quantumlib/Cirq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquantum_fourier_transform.py
75 lines (61 loc) · 2.68 KB
/
quantum_fourier_transform.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# pylint: disable=wrong-or-nonexistent-copyright-notice
"""Creates and simulates a circuit for Quantum Fourier Transform(QFT) on 4 qubits.
In this example we demonstrate Fourier Transform on
(1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0) vector. To do the same, we prepare the input state of the
qubits as |0000>.
=== EXAMPLE OUTPUT ===
Circuit:
(0, 0): ─H───@^0.5───×───H────────────@^0.5─────×───H──────────@^0.5──×─H
│ │ │ │ │ │
(0, 1): ─────@───────×───@^0.25───×───@─────────×───@^0.25───×──@─────×──
│ │ │ │
(1, 0): ─────────────────┼────────┼───@^0.125───×───┼────────┼───────────
│ │ │ │ │ │
(1, 1): ─────────────────@────────×───@─────────×───@────────×───────────
FinalState
[0.25+0.j 0.25+0.j 0.25+0.j 0.25+0.j 0.25+0.j 0.25+0.j 0.25+0.j 0.25+0.j
0.25+0.j 0.25+0.j 0.25+0.j 0.25+0.j 0.25+0.j 0.25+0.j 0.25+0.j 0.25+0.j]
"""
import numpy as np
import cirq
def main():
"""Demonstrates Quantum Fourier transform."""
# Create circuit
qft_circuit = generate_2x2_grid_qft_circuit()
print('Circuit:')
print(qft_circuit)
# Simulate and collect final_state
simulator = cirq.Simulator()
result = simulator.simulate(qft_circuit)
print()
print('FinalState')
print(np.around(result.final_state_vector, 3))
def _cz_and_swap(q0, q1, rot):
yield cirq.CZ(q0, q1) ** rot
yield cirq.SWAP(q0, q1)
# Create a quantum fourier transform circuit for 2*2 planar qubit architecture.
# Circuit is adopted from https://arxiv.org/pdf/quant-ph/0402196.pdf
def generate_2x2_grid_qft_circuit():
# Define a 2*2 square grid of qubits.
a, b, c, d = [
cirq.GridQubit(0, 0),
cirq.GridQubit(0, 1),
cirq.GridQubit(1, 1),
cirq.GridQubit(1, 0),
]
circuit = cirq.Circuit(
cirq.H(a),
_cz_and_swap(a, b, 0.5),
_cz_and_swap(b, c, 0.25),
_cz_and_swap(c, d, 0.125),
cirq.H(a),
_cz_and_swap(a, b, 0.5),
_cz_and_swap(b, c, 0.25),
cirq.H(a),
_cz_and_swap(a, b, 0.5),
cirq.H(a),
strategy=cirq.InsertStrategy.EARLIEST,
)
return circuit
if __name__ == '__main__':
main()