-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcase1_exact_binary.py
More file actions
101 lines (79 loc) · 2.87 KB
/
case1_exact_binary.py
File metadata and controls
101 lines (79 loc) · 2.87 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
"""
Case 1: Exact Binary Phase
Input: eigenstate = |1>, n = 3, theta = 1/8
Expected Output: Bitstring '100' (little-endian) = 001 (big-endian)
Estimated theta = 0.125 (Exact)
"""
import numpy as np
from qiskit import QuantumCircuit, transpile
from qiskit_aer import AerSimulator
from iqft_core import qft, iqft
def case1_exact_binary():
"""
Test IQFT with exact binary phase theta = 1/8.
Phase can be exactly represented with 3 qubits.
"""
print("=" * 60)
print("CASE 1: EXACT BINARY PHASE")
print("=" * 60)
n = 3 # Number of qubits
eigenstate = 1 # |1>
theta = 1 / 8 # Phase value
print(f"Parameters:")
print(f" n = {n} qubits")
print(f" eigenstate = |{eigenstate}>")
print(f" theta = {theta} = 1/8")
print(f" Binary representation: {format(int(theta * 2**n), f'0{n}b')}")
print()
# Create quantum circuit
qc = QuantumCircuit(n, n)
# Initialize eigenstate |1>
qc.x(0) # Qiskit uses little-endian, so qubit 0 is LSB
# Apply QFT to encode the phase
qft(qc, n)
# Apply phase encoding: multiply each basis state by e^(2*pi*i*theta*k)
# For exact binary phases, we encode by rotating specific qubits
# theta = 1/8 = 0.001 in binary, so we rotate qubit 2 (MSB)
k = int(theta * (2 ** n)) # k = 1 for theta = 1/8
for i in range(n):
if (k >> i) & 1:
qc.p(np.pi, i)
# Apply IQFT to decode the phase
iqft(qc, n)
# Measure all qubits
qc.measure(range(n), range(n))
print("Circuit created with QFT -> Phase Encoding -> IQFT")
print()
# Simulate the circuit
simulator = AerSimulator()
compiled_circuit = transpile(qc, simulator)
job = simulator.run(compiled_circuit, shots=1024)
result = job.result()
counts = result.get_counts()
print("Measurement Results:")
print(f" Counts: {counts}")
print()
# Get the most frequent bitstring
most_frequent = max(counts, key=counts.get)
print(f"Most frequent bitstring: '{most_frequent}' (little-endian)")
# Convert to big-endian and estimate theta
big_endian = most_frequent[::-1]
estimated_value = int(big_endian, 2)
estimated_theta = estimated_value / (2 ** n)
print(f"Big-endian representation: '{big_endian}'")
print(f"Decimal value: {estimated_value}")
print(f"Estimated theta = {estimated_theta}")
print()
# Verify result
expected_bitstring = '100' # Little-endian for 001 big-endian
if most_frequent == expected_bitstring:
print("[SUCCESS] RESULT: EXACT MATCH!")
print(f" Expected: '{expected_bitstring}' -> theta = {theta}")
print(f" Measured: '{most_frequent}' -> theta = {estimated_theta}")
else:
print(f"[FAIL] Result differs from expected '{expected_bitstring}'")
print("=" * 60)
print()
return counts, estimated_theta
if __name__ == "__main__":
case1_exact_binary()