-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiqft_core.py
More file actions
106 lines (80 loc) · 2.62 KB
/
iqft_core.py
File metadata and controls
106 lines (80 loc) · 2.62 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
"""
Core IQFT Implementation Module
Provides QFT and IQFT circuit building functions
"""
import numpy as np
from qiskit import QuantumCircuit
def qft(circuit, n):
"""
Applies Quantum Fourier Transform to the first n qubits in circuit.
Args:
circuit: QuantumCircuit object
n: Number of qubits to apply QFT
Returns:
Modified circuit with QFT applied
"""
for i in range(n):
# Apply Hadamard gate
circuit.h(i)
# Apply controlled phase rotations
for j in range(i + 1, n):
angle = 2 * np.pi / (2 ** (j - i + 1))
circuit.cp(angle, j, i)
# Swap qubits to reverse the order
for i in range(n // 2):
circuit.swap(i, n - i - 1)
return circuit
def iqft(circuit, n):
"""
Applies Inverse Quantum Fourier Transform to the first n qubits in circuit.
Uses negative rotation angles compared to QFT.
Args:
circuit: QuantumCircuit object
n: Number of qubits to apply IQFT
Returns:
Modified circuit with IQFT applied
"""
# Swap qubits first (reverse of QFT)
for i in range(n // 2):
circuit.swap(i, n - i - 1)
# Apply inverse rotations (in reverse order with negative angles)
for i in range(n - 1, -1, -1):
# Apply controlled phase rotations with negative angles
for j in range(n - 1, i, -1):
angle = -2 * np.pi / (2 ** (j - i + 1))
circuit.cp(angle, j, i)
# Apply Hadamard gate
circuit.h(i)
return circuit
def create_eigenstate(n, eigenvalue):
"""
Create a computational basis state |eigenvalue⟩.
Args:
n: Number of qubits
eigenvalue: Integer value of the eigenstate
Returns:
QuantumCircuit initialized to |eigenvalue⟩
"""
qc = QuantumCircuit(n)
# Convert eigenvalue to binary and apply X gates
binary = format(eigenvalue, f'0{n}b')
for i, bit in enumerate(binary):
if bit == '1':
qc.x(n - 1 - i) # Qiskit uses little-endian
return qc
def apply_phase_gate(circuit, n, eigenstate, theta):
"""
Apply unitary evolution e^(2πiθ) to encode phase information.
Uses controlled rotations to encode the phase.
Args:
circuit: QuantumCircuit object
n: Number of qubits
eigenstate: Eigenstate value
theta: Phase value to encode (between 0 and 1)
"""
phase_angle = 2 * np.pi * theta
# Apply global phase rotation (simplified approach)
# For each qubit, apply appropriate phase based on theta
for i in range(n):
circuit.p(phase_angle * (2 ** i), i)
return circuit