Skip to content

Conversation

@splch
Copy link
Owner

@splch splch commented Aug 11, 2023

steps to reproduce:

import numpy as np
from qubit_simulator import QubitSimulator


def apply_qft(simulator):
    num_qubits = simulator.num_qubits
    for target_qubit in range(num_qubits):
        # Apply Hadamard gate to the target qubit
        simulator.H(target_qubit)
        # Apply controlled phase gates to the target qubit
        for control_qubit in range(target_qubit + 1, num_qubits):
            phase_angle = 2 * np.pi / (2 ** (control_qubit - target_qubit + 1))
            simulator.CU(target_qubit, control_qubit, 0, 0, phase_angle)


simulator = QubitSimulator(3)

# Create a random initial state vector and normalize it
random_state = np.random.rand(8) + 1j * np.random.rand(8)
random_state /= np.linalg.norm(random_state)

# Set the random state as the initial state in the simulator
simulator.state_vector = random_state.copy()

# Apply QFT in the simulator
apply_qft(simulator)

# Compute the expected result using NumPy's FFT and normalize
fft_result = np.fft.fft(random_state) / np.sqrt(8)

# Compare the state vectors
print(f"simulator = {simulator.state_vector}")
print(f"np.fft.fft = {fft_result}")

simulator = [ 0.7119692 +0.47623858j -0.28574069-0.11805649j 0.24575554+0.09035911j
-0.04866791-0.15844743j -0.12457145-0.00268797j -0.02088207-0.09257858j
0.18633583+0.01814583j 0.04555531-0.11410155j]
np.fft.fft = [ 0.7119692 +0.47623858j 0.04555531-0.11410155j -0.04866791-0.15844743j
-0.02088207-0.09257858j -0.28574069-0.11805649j 0.18633583+0.01814583j
0.24575554+0.09035911j -0.12457145-0.00268797j]

@splch splch merged commit 312c56a into main Aug 11, 2023
@splch splch deleted the fix-qft-test branch August 11, 2023 23:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants