[3]:
import numpy as np
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister, transpile, assemble
from qiskit.visualization import plot_histogram
from qiskit_aer import AerSimulator
import matplotlib.pyplot as plt
from fixed_counts_to_2d_array import counts_to_2d_array

def initialize_wavefunction(qc, qubits, wave_function):
    """Initialize the quantum circuit with the given wave function."""
    for i, amplitude in enumerate(wave_function):
        qc.ry(2 * np.arccos(amplitude), qubits[i])

def apply_phase_shift(qc, qubits, phase_shifts):
    """Apply phase shift to simulate the weak phase object."""
    for i, phase in enumerate(phase_shifts):
        qc.rz(phase, qubits[i])

def simulate_electron_wave(phase_shifts, n_qubits=4):
    # Initialize quantum circuit
    qr = QuantumRegister(n_qubits, 'q')
    cr = ClassicalRegister(n_qubits, 'c')
    qc = QuantumCircuit(qr, cr)

    # Define initial wave function (Gaussian-like distribution)
    initial_wave_function = np.array([np.exp(-0.5 * (i - n_qubits / 2) ** 2) for i in range(n_qubits)])
    initial_wave_function /= np.linalg.norm(initial_wave_function)

    print("Initial wave function:", initial_wave_function)  # Debug print

    # Initialize wave function in the quantum circuit
    initialize_wavefunction(qc, qr, initial_wave_function)

    # Apply phase shift to simulate the weak phase object
    apply_phase_shift(qc, qr, phase_shifts)

    # Measure the results
    qc.measure(qr, cr)

    print("Quantum circuit:\n", qc)  # Debug print

    return qc

# Define phase shifts to simulate the weak phase object
phase_shifts = [0.1, 0.2, 0.3, 0.4]

# Simulate electron wave
qc = simulate_electron_wave(phase_shifts, n_qubits=4)

# Execute the circuit using AerSimulator
simulator = AerSimulator()
compiled_circuit = transpile(qc, simulator)
print("Compiled circuit:\n", compiled_circuit)  # Debug print
sim_result = simulator.run(compiled_circuit).result()
counts = sim_result.get_counts()

print("Counts:", counts)  # Debug print

# Manually plot the histogram
if counts:
    outcomes = list(counts.keys())
    values = list(counts.values())

    plt.figure(figsize=(10, 5))
    plt.bar(outcomes, values)
    plt.title("Electron Wave Simulation Results")
    plt.xlabel("Measurement Outcome")
    plt.ylabel("Counts")
    plt.xticks(rotation=90)
    plt.show()
else:
    print("No counts to display.")

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[3], line 6
      4 from qiskit_aer import AerSimulator
      5 import matplotlib.pyplot as plt
----> 6 from fixed_counts_to_2d_array import counts_to_2d_array
      8 def initialize_wavefunction(qc, qubits, wave_function):
      9     """Initialize the quantum circuit with the given wave function."""

ModuleNotFoundError: No module named 'fixed_counts_to_2d_array'
[ ]:
#expandig to 2D
[2]:
import numpy as np
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister, transpile, assemble
from qiskit.visualization import plot_histogram
from qiskit_aer import AerSimulator
import matplotlib.pyplot as plt

def initialize_wavefunction_2d(qc, qubits, wave_function):
    """Initialize the quantum circuit with the given 2D wave function."""
    for i, amplitude in enumerate(wave_function.flatten()):
        qc.ry(2 * np.arccos(amplitude), qubits[i])

def apply_phase_shift_2d(qc, qubits, phase_shifts):
    """Apply phase shift to simulate the weak phase object in 2D."""
    for i, phase in enumerate(phase_shifts.flatten()):
        qc.rz(phase, qubits[i])

def simulate_electron_wave_2d(phase_shifts, grid_size=(4, 4)):
    n_qubits = grid_size[0] * grid_size[1]

    # Initialize quantum circuit
    qr = QuantumRegister(n_qubits, 'q')
    cr = ClassicalRegister(n_qubits, 'c')
    qc = QuantumCircuit(qr, cr)

    # Define initial 2D wave function (Gaussian-like distribution)
    x = np.linspace(-1, 1, grid_size[0])
    y = np.linspace(-1, 1, grid_size[1])
    X, Y = np.meshgrid(x, y)
    initial_wave_function = np.exp(-0.5 * (X**2 + Y**2))
    initial_wave_function /= np.linalg.norm(initial_wave_function)

    print("Initial 2D wave function:\n", initial_wave_function)  # Debug print

    # Initialize wave function in the quantum circuit
    initialize_wavefunction_2d(qc, qr, initial_wave_function)

    # Apply phase shift to simulate the weak phase object
    apply_phase_shift_2d(qc, qr, phase_shifts)

    # Measure the results
    qc.measure(qr, cr)

    print("Quantum circuit:\n", qc)  # Debug print

    return qc, initial_wave_function

# Define phase shifts to simulate the weak phase object in 2D
phase_shifts = np.array([[0.1, 0.2, 0.3, 0.4],
                         [0.2, 0.3, 0.4, 0.5],
                         [0.3, 0.4, 0.5, 0.6],
                         [0.4, 0.5, 0.6, 0.7]])

# Simulate electron wave in 2D
qc, initial_wave_function = simulate_electron_wave_2d(phase_shifts, grid_size=(4, 4))

# Execute the circuit using AerSimulator
simulator = AerSimulator()
compiled_circuit = transpile(qc, simulator)
print("Compiled circuit:\n", compiled_circuit)  # Debug print
sim_result = simulator.run(compiled_circuit).result()
counts = sim_result.get_counts()

print("Counts:", counts)  # Debug print

# Verify the number of qubits
n_qubits = 4 * 4
bitstring_length = len(list(counts.keys())[0])
print(f"Expected number of qubits: {n_qubits}")
print(f"Actual bitstring length: {bitstring_length}")

# Convert counts to 2D array for visualization
def counts_to_2d_array(counts, grid_size):
    n_qubits = grid_size[0] * grid_size[1]
    result_array = np.zeros((grid_size[0], grid_size[1]))

    for bitstring, count in counts.items():
        # Truncate the bitstring to the expected length
        truncated_bitstring = bitstring[-n_qubits:]
        index = int(truncated_bitstring, 2)
        row = index // grid_size[1]
        col = index % grid_size[1]
        print(f"Bitstring: {truncated_bitstring}, Index: {index}, Row: {row}, Col: {col}, Count: {count}")  # Debug print
        result_array[row, col] = count

    return result_array

result_array = counts_to_2d_array(counts, (4, 4))

print("Result array:\n", result_array)  # Debug print

# Plot the initial wave function and the result
fig, ax = plt.subplots(1, 2, figsize=(12, 6))

ax[0].imshow(initial_wave_function, cmap='viridis', origin='lower')
ax[0].set_title("Initial 2D Wave Function")
ax[0].axis('off')

ax[1].imshow(result_array, cmap='viridis', origin='lower')
ax[1].set_title("Electron Wave Simulation Results")
ax[1].axis('off')

plt.show()

Initial 2D wave function:
 [[0.14566959 0.22718971 0.22718971 0.14566959]
 [0.22718971 0.35433041 0.35433041 0.22718971]
 [0.22718971 0.35433041 0.35433041 0.22718971]
 [0.14566959 0.22718971 0.22718971 0.14566959]]
Quantum circuit:
       ┌────────────┐┌─────────┐┌─┐
 q_0: ┤ Ry(2.8492) ├┤ Rz(0.1) ├┤M├─────────────────────────────────────────────
      ├────────────┤├─────────┤└╥┘┌─┐
 q_1: ┤ Ry(2.6832) ├┤ Rz(0.2) ├─╫─┤M├──────────────────────────────────────────
      ├────────────┤├─────────┤ ║ └╥┘┌─┐
 q_2: ┤ Ry(2.6832) ├┤ Rz(0.3) ├─╫──╫─┤M├───────────────────────────────────────
      ├────────────┤├─────────┤ ║  ║ └╥┘┌─┐
 q_3: ┤ Ry(2.8492) ├┤ Rz(0.4) ├─╫──╫──╫─┤M├────────────────────────────────────
      ├────────────┤├─────────┤ ║  ║  ║ └╥┘┌─┐
 q_4: ┤ Ry(2.6832) ├┤ Rz(0.2) ├─╫──╫──╫──╫─┤M├─────────────────────────────────
      ├────────────┤├─────────┤ ║  ║  ║  ║ └╥┘┌─┐
 q_5: ┤ Ry(2.4172) ├┤ Rz(0.3) ├─╫──╫──╫──╫──╫─┤M├──────────────────────────────
      ├────────────┤├─────────┤ ║  ║  ║  ║  ║ └╥┘┌─┐
 q_6: ┤ Ry(2.4172) ├┤ Rz(0.4) ├─╫──╫──╫──╫──╫──╫─┤M├───────────────────────────
      ├────────────┤├─────────┤ ║  ║  ║  ║  ║  ║ └╥┘┌─┐
 q_7: ┤ Ry(2.6832) ├┤ Rz(0.5) ├─╫──╫──╫──╫──╫──╫──╫─┤M├────────────────────────
      ├────────────┤├─────────┤ ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
 q_8: ┤ Ry(2.6832) ├┤ Rz(0.3) ├─╫──╫──╫──╫──╫──╫──╫──╫─┤M├─────────────────────
      ├────────────┤├─────────┤ ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
 q_9: ┤ Ry(2.4172) ├┤ Rz(0.4) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├──────────────────
      ├────────────┤├─────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_10: ┤ Ry(2.4172) ├┤ Rz(0.5) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├───────────────
      ├────────────┤├─────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_11: ┤ Ry(2.6832) ├┤ Rz(0.6) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├────────────
      ├────────────┤├─────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_12: ┤ Ry(2.8492) ├┤ Rz(0.4) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├─────────
      ├────────────┤├─────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_13: ┤ Ry(2.6832) ├┤ Rz(0.5) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├──────
      ├────────────┤├─────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_14: ┤ Ry(2.6832) ├┤ Rz(0.6) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├───
      ├────────────┤├─────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_15: ┤ Ry(2.8492) ├┤ Rz(0.7) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├
      └────────────┘└─────────┘ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘
c: 16/══════════════════════════╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩═
                                0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15
Compiled circuit:
 global phase: 3.0832
      ┌──────────────────┐┌─┐
 q_0: ┤ U3(2.8492,0.1,0) ├┤M├─────────────────────────────────────────────
      ├──────────────────┤└╥┘┌─┐
 q_1: ┤ U3(2.6832,0.2,0) ├─╫─┤M├──────────────────────────────────────────
      ├──────────────────┤ ║ └╥┘┌─┐
 q_2: ┤ U3(2.6832,0.3,0) ├─╫──╫─┤M├───────────────────────────────────────
      ├──────────────────┤ ║  ║ └╥┘┌─┐
 q_3: ┤ U3(2.8492,0.4,0) ├─╫──╫──╫─┤M├────────────────────────────────────
      ├──────────────────┤ ║  ║  ║ └╥┘┌─┐
 q_4: ┤ U3(2.6832,0.2,0) ├─╫──╫──╫──╫─┤M├─────────────────────────────────
      ├──────────────────┤ ║  ║  ║  ║ └╥┘┌─┐
 q_5: ┤ U3(2.4172,0.3,0) ├─╫──╫──╫──╫──╫─┤M├──────────────────────────────
      ├──────────────────┤ ║  ║  ║  ║  ║ └╥┘┌─┐
 q_6: ┤ U3(2.4172,0.4,0) ├─╫──╫──╫──╫──╫──╫─┤M├───────────────────────────
      ├──────────────────┤ ║  ║  ║  ║  ║  ║ └╥┘┌─┐
 q_7: ┤ U3(2.6832,0.5,0) ├─╫──╫──╫──╫──╫──╫──╫─┤M├────────────────────────
      ├──────────────────┤ ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
 q_8: ┤ U3(2.6832,0.3,0) ├─╫──╫──╫──╫──╫──╫──╫──╫─┤M├─────────────────────
      ├──────────────────┤ ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
 q_9: ┤ U3(2.4172,0.4,0) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├──────────────────
      ├──────────────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_10: ┤ U3(2.4172,0.5,0) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├───────────────
      ├──────────────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_11: ┤ U3(2.6832,0.6,0) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├────────────
      ├──────────────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_12: ┤ U3(2.8492,0.4,0) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├─────────
      ├──────────────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_13: ┤ U3(2.6832,0.5,0) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├──────
      ├──────────────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_14: ┤ U3(2.6832,0.6,0) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├───
      ├──────────────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_15: ┤ U3(2.8492,0.7,0) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├
      └──────────────────┘ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘
c: 16/═════════════════════╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩═
                           0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15
Counts: {'1111111110001111': 1, '1110111111110111': 1, '1011111111110011': 1, '1111110111100111': 1, '1111111010011111': 1, '1111110010111011': 1, '1011101110111111': 1, '1111001101111111': 1, '1101101111111111': 1, '1101111111101111': 1, '1111111110110011': 1, '1011111010111111': 1, '1111110101111011': 1, '1111100111011111': 1, '1011101111111011': 2, '1010111111011111': 1, '1111101001101111': 1, '1111111000111111': 1, '1111110100111111': 1, '1011101111111111': 2, '1101011111011111': 1, '1111101011111111': 1, '1101111110111101': 1, '1111101111111110': 1, '1011111111110111': 1, '1111110010111111': 1, '0111111110111111': 1, '1110111100111111': 1, '1101110110111111': 1, '1111110111011101': 1, '1011101111011111': 2, '1111011101111111': 1, '1111101101111111': 1, '1111110111011011': 1, '1111101101111101': 1, '1101111111111110': 2, '1111110111101111': 3, '1011100100111111': 1, '1111001111011111': 1, '1111101101111011': 1, '1111101111110111': 2, '1011110110111111': 1, '1110111111111111': 12, '0110111111111111': 1, '1111111111011110': 1, '1011111111111101': 1, '1111001110111111': 1, '0111101110111111': 1, '1111111011111011': 1, '1111111110011111': 6, '1111111110010111': 1, '1111111101101101': 1, '1111011111111011': 1, '1111110111111110': 1, '1111111111011101': 5, '1111111101111011': 1, '1011110111111111': 5, '1101100111111111': 1, '1101111001111111': 1, '1111110110111011': 1, '1111100110111011': 1, '1101011111111111': 2, '1111111101111111': 8, '1011110111011111': 1, '1110110111011111': 1, '1111111010111111': 3, '1110101111011111': 1, '1111011111101111': 2, '1011111110111111': 1, '1011111111011111': 2, '1011011111111101': 1, '1111111101110111': 1, '1110111110111111': 1, '1111110110011111': 4, '0111111110011111': 1, '0111101111111111': 2, '1111101111111101': 4, '1110101110111111': 1, '1101111111111111': 20, '1110101111111111': 2, '1011111111010111': 1, '1111010111111111': 6, '1011111011111111': 1, '1111101110111111': 7, '1111111111001111': 2, '1111110111111111': 55, '1101011101111111': 1, '0111111011111101': 1, '1111011111111110': 1, '1111111011011111': 3, '1110110100111101': 1, '0111111111111101': 1, '0011111111111111': 2, '0111101111110111': 1, '1111111011111111': 25, '1111011111011111': 5, '1011001110111101': 1, '1101101110111111': 1, '1111111110110111': 2, '1011011111011111': 1, '1111110101111111': 3, '1111101111101011': 1, '1111110111110111': 2, '1111101010111111': 2, '1111111110011101': 1, '0111111111011111': 1, '1111101111101111': 1, '1111111111100111': 2, '1101110111111111': 2, '1110111111011111': 1, '1111111111111110': 9, '1111110111111101': 2, '1111011110111111': 6, '1111100110111111': 1, '1111111111011111': 50, '1111100011111111': 2, '1110110111101111': 1, '1111101101011111': 3, '1111101111111011': 1, '1111111110111101': 4, '0101111110101111': 1, '0111111111111111': 7, '1111101111111111': 52, '1111000111111111': 2, '0111101010111111': 1, '1111111110111111': 54, '1111010111011111': 1, '1111011110111101': 1, '1111111111011011': 2, '1111010101111111': 1, '1110101110111011': 1, '1011111101011111': 1, '1111111110111011': 2, '1111011111111111': 11, '1111111111101111': 16, '1111111111111000': 1, '1111111100101111': 1, '1111110111011111': 9, '0111011100111111': 1, '1101101111011111': 1, '1111111101011111': 1, '1101111111011111': 5, '1101111111111011': 2, '1111111111111101': 21, '1101111110111111': 6, '1111110011111111': 4, '1111111001111111': 1, '1011111101111111': 1, '1111111100111111': 3, '1111111101101111': 2, '0111111111111011': 2, '1111001100011111': 1, '1111111110101111': 2, '0111011011111111': 1, '1111111111110111': 5, '1111110111111011': 4, '1111110110111111': 11, '1011111111111111': 20, '1111101100101111': 1, '1111101111011111': 10, '1111100111111111': 6, '1111111101111101': 2, '1111111111111011': 30, '1111111111111111': 354}
Expected number of qubits: 16
Actual bitstring length: 16
Bitstring: 1111111110001111, Index: 65423, Row: 16355, Col: 3, Count: 1
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[2], line 87
     83         result_array[row, col] = count
     85     return result_array
---> 87 result_array = counts_to_2d_array(counts, (4, 4))
     89 print("Result array:\n", result_array)  # Debug print
     91 # Plot the initial wave function and the result

Cell In[2], line 83, in counts_to_2d_array(counts, grid_size)
     81     col = index % grid_size[1]
     82     print(f"Bitstring: {truncated_bitstring}, Index: {index}, Row: {row}, Col: {col}, Count: {count}")  # Debug print
---> 83     result_array[row, col] = count
     85 return result_array

IndexError: index 16355 is out of bounds for axis 0 with size 4
[ ]:
import numpy as np
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister, transpile, assemble
from qiskit.visualization import plot_histogram
from qiskit_aer import AerSimulator
import matplotlib.pyplot as plt

def initialize_wavefunction_2d(qc, qubits, wave_function):
    """Initialize the quantum circuit with the given 2D wave function."""
    for i, amplitude in enumerate(wave_function.flatten()):
        qc.ry(2 * np.arccos(amplitude), qubits[i])

def apply_phase_shift_2d(qc, qubits, phase_shifts):
    """Apply phase shift to simulate the weak phase object in 2D."""
    for i, phase in enumerate(phase_shifts.flatten()):
        qc.rz(phase, qubits[i])

def simulate_electron_wave_2d(phase_shifts, grid_size=(4, 4)):
    n_qubits = grid_size[0] * grid_size[1]

    # Initialize quantum circuit
    qr = QuantumRegister(n_qubits, 'q')
    cr = ClassicalRegister(n_qubits, 'c')
    qc = QuantumCircuit(qr, cr)

    # Define initial 2D wave function (Gaussian-like distribution)
    x = np.linspace(-1, 1, grid_size[0])
    y = np.linspace(-1, 1, grid_size[1])
    X, Y = np.meshgrid(x, y)
    initial_wave_function = np.exp(-0.5 * (X**2 + Y**2))
    initial_wave_function /= np.linalg.norm(initial_wave_function)

    print("Initial 2D wave function:\n", initial_wave_function)  # Debug print

    # Initialize wave function in the quantum circuit
    initialize_wavefunction_2d(qc, qr, initial_wave_function)

    # Apply phase shift to simulate the weak phase object
    apply_phase_shift_2d(qc, qr, phase_shifts)

    # Measure the results
    qc.measure(qr, cr)

    print("Quantum circuit:\n", qc)  # Debug print

    return qc, initial_wave_function

# Define phase shifts to simulate the weak phase object in 2D
phase_shifts = np.array([[0.1, 0.2, 0.3, 0.4],
                         [0.2, 0.3, 0.4, 0.5],
                         [0.3, 0.4, 0.5, 0.6],
                         [0.4, 0.5, 0.6, 0.7]])

# Simulate electron wave in 2D
qc, initial_wave_function = simulate_electron_wave_2d(phase_shifts, grid_size=(4, 4))

# Execute the circuit using AerSimulator
simulator = AerSimulator()
compiled_circuit = transpile(qc, simulator)
print("Compiled circuit:\n", compiled_circuit)  # Debug print
sim_result = simulator.run(compiled_circuit).result()
counts = sim_result.get_counts()

print("Counts:", counts)  # Debug print

# Verify the number of qubits
n_qubits = 4 * 4
bitstring_length = len(list(counts.keys())[0])
print(f"Expected number of qubits: {n_qubits}")
print(f"Actual bitstring length: {bitstring_length}")

# Convert counts to 2D array for visualization
def counts_to_2d_array(counts, grid_size):
    n_qubits = grid_size[0] * grid_size[1]
    result_array = np.zeros((grid_size[0], grid_size[1]))

    for bitstring, count in counts.items():
        # Truncate the bitstring to the expected length
        truncated_bitstring = bitstring[-n_qubits:]
        index = int(truncated_bitstring, 2)
        row = index // grid_size[1]
        col = index % grid_size[1]
        print(f"Bitstring: {truncated_bitstring}, Index: {index}, Row: {row}, Col: {col}, Count: {count}")  # Debug print
        result_array[row, col] = count

    return result_array

result_array = counts_to_2d_array(counts, (4, 4))

print("Result array:\n", result_array)  # Debug print

# Plot the initial wave function and the result
fig, ax = plt.subplots(1, 2, figsize=(12, 6))

ax[0].imshow(initial_wave_function, cmap='viridis', origin='lower')
ax[0].set_title("Initial 2D Wave Function")
ax[0].axis('off')

ax[1].imshow(result_array, cmap='viridis', origin='lower')
ax[1].set_title("Electron Wave Simulation Results")
ax[1].axis('off')

plt.show()

Initial 2D wave function:
 [[0.14566959 0.22718971 0.22718971 0.14566959]
 [0.22718971 0.35433041 0.35433041 0.22718971]
 [0.22718971 0.35433041 0.35433041 0.22718971]
 [0.14566959 0.22718971 0.22718971 0.14566959]]
Quantum circuit:
       ┌────────────┐┌─────────┐┌─┐
 q_0: ┤ Ry(2.8492) ├┤ Rz(0.1) ├┤M├─────────────────────────────────────────────
      ├────────────┤├─────────┤└╥┘┌─┐
 q_1: ┤ Ry(2.6832) ├┤ Rz(0.2) ├─╫─┤M├──────────────────────────────────────────
      ├────────────┤├─────────┤ ║ └╥┘┌─┐
 q_2: ┤ Ry(2.6832) ├┤ Rz(0.3) ├─╫──╫─┤M├───────────────────────────────────────
      ├────────────┤├─────────┤ ║  ║ └╥┘┌─┐
 q_3: ┤ Ry(2.8492) ├┤ Rz(0.4) ├─╫──╫──╫─┤M├────────────────────────────────────
      ├────────────┤├─────────┤ ║  ║  ║ └╥┘┌─┐
 q_4: ┤ Ry(2.6832) ├┤ Rz(0.2) ├─╫──╫──╫──╫─┤M├─────────────────────────────────
      ├────────────┤├─────────┤ ║  ║  ║  ║ └╥┘┌─┐
 q_5: ┤ Ry(2.4172) ├┤ Rz(0.3) ├─╫──╫──╫──╫──╫─┤M├──────────────────────────────
      ├────────────┤├─────────┤ ║  ║  ║  ║  ║ └╥┘┌─┐
 q_6: ┤ Ry(2.4172) ├┤ Rz(0.4) ├─╫──╫──╫──╫──╫──╫─┤M├───────────────────────────
      ├────────────┤├─────────┤ ║  ║  ║  ║  ║  ║ └╥┘┌─┐
 q_7: ┤ Ry(2.6832) ├┤ Rz(0.5) ├─╫──╫──╫──╫──╫──╫──╫─┤M├────────────────────────
      ├────────────┤├─────────┤ ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
 q_8: ┤ Ry(2.6832) ├┤ Rz(0.3) ├─╫──╫──╫──╫──╫──╫──╫──╫─┤M├─────────────────────
      ├────────────┤├─────────┤ ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
 q_9: ┤ Ry(2.4172) ├┤ Rz(0.4) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├──────────────────
      ├────────────┤├─────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_10: ┤ Ry(2.4172) ├┤ Rz(0.5) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├───────────────
      ├────────────┤├─────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_11: ┤ Ry(2.6832) ├┤ Rz(0.6) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├────────────
      ├────────────┤├─────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_12: ┤ Ry(2.8492) ├┤ Rz(0.4) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├─────────
      ├────────────┤├─────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_13: ┤ Ry(2.6832) ├┤ Rz(0.5) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├──────
      ├────────────┤├─────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_14: ┤ Ry(2.6832) ├┤ Rz(0.6) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├───
      ├────────────┤├─────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_15: ┤ Ry(2.8492) ├┤ Rz(0.7) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├
      └────────────┘└─────────┘ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘
c: 16/══════════════════════════╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩═
                                0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15
Compiled circuit:
 global phase: 3.0832
      ┌──────────────────┐┌─┐
 q_0: ┤ U3(2.8492,0.1,0) ├┤M├─────────────────────────────────────────────
      ├──────────────────┤└╥┘┌─┐
 q_1: ┤ U3(2.6832,0.2,0) ├─╫─┤M├──────────────────────────────────────────
      ├──────────────────┤ ║ └╥┘┌─┐
 q_2: ┤ U3(2.6832,0.3,0) ├─╫──╫─┤M├───────────────────────────────────────
      ├──────────────────┤ ║  ║ └╥┘┌─┐
 q_3: ┤ U3(2.8492,0.4,0) ├─╫──╫──╫─┤M├────────────────────────────────────
      ├──────────────────┤ ║  ║  ║ └╥┘┌─┐
 q_4: ┤ U3(2.6832,0.2,0) ├─╫──╫──╫──╫─┤M├─────────────────────────────────
      ├──────────────────┤ ║  ║  ║  ║ └╥┘┌─┐
 q_5: ┤ U3(2.4172,0.3,0) ├─╫──╫──╫──╫──╫─┤M├──────────────────────────────
      ├──────────────────┤ ║  ║  ║  ║  ║ └╥┘┌─┐
 q_6: ┤ U3(2.4172,0.4,0) ├─╫──╫──╫──╫──╫──╫─┤M├───────────────────────────
      ├──────────────────┤ ║  ║  ║  ║  ║  ║ └╥┘┌─┐
 q_7: ┤ U3(2.6832,0.5,0) ├─╫──╫──╫──╫──╫──╫──╫─┤M├────────────────────────
      ├──────────────────┤ ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
 q_8: ┤ U3(2.6832,0.3,0) ├─╫──╫──╫──╫──╫──╫──╫──╫─┤M├─────────────────────
      ├──────────────────┤ ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
 q_9: ┤ U3(2.4172,0.4,0) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├──────────────────
      ├──────────────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_10: ┤ U3(2.4172,0.5,0) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├───────────────
      ├──────────────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_11: ┤ U3(2.6832,0.6,0) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├────────────
      ├──────────────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_12: ┤ U3(2.8492,0.4,0) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├─────────
      ├──────────────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_13: ┤ U3(2.6832,0.5,0) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├──────
      ├──────────────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_14: ┤ U3(2.6832,0.6,0) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├───
      ├──────────────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_15: ┤ U3(2.8492,0.7,0) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├
      └──────────────────┘ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘
c: 16/═════════════════════╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩═
                           0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15
Counts: {'1111111110001111': 1, '1110111111110111': 1, '1011111111110011': 1, '1111110111100111': 1, '1111111010011111': 1, '1111110010111011': 1, '1011101110111111': 1, '1111001101111111': 1, '1101101111111111': 1, '1101111111101111': 1, '1111111110110011': 1, '1011111010111111': 1, '1111110101111011': 1, '1111100111011111': 1, '1011101111111011': 2, '1010111111011111': 1, '1111101001101111': 1, '1111111000111111': 1, '1111110100111111': 1, '1011101111111111': 2, '1101011111011111': 1, '1111101011111111': 1, '1101111110111101': 1, '1111101111111110': 1, '1011111111110111': 1, '1111110010111111': 1, '0111111110111111': 1, '1110111100111111': 1, '1101110110111111': 1, '1111110111011101': 1, '1011101111011111': 2, '1111011101111111': 1, '1111101101111111': 1, '1111110111011011': 1, '1111101101111101': 1, '1101111111111110': 2, '1111110111101111': 3, '1011100100111111': 1, '1111001111011111': 1, '1111101101111011': 1, '1111101111110111': 2, '1011110110111111': 1, '1110111111111111': 12, '0110111111111111': 1, '1111111111011110': 1, '1011111111111101': 1, '1111001110111111': 1, '0111101110111111': 1, '1111111011111011': 1, '1111111110011111': 6, '1111111110010111': 1, '1111111101101101': 1, '1111011111111011': 1, '1111110111111110': 1, '1111111111011101': 5, '1111111101111011': 1, '1011110111111111': 5, '1101100111111111': 1, '1101111001111111': 1, '1111110110111011': 1, '1111100110111011': 1, '1101011111111111': 2, '1111111101111111': 8, '1011110111011111': 1, '1110110111011111': 1, '1111111010111111': 3, '1110101111011111': 1, '1111011111101111': 2, '1011111110111111': 1, '1011111111011111': 2, '1011011111111101': 1, '1111111101110111': 1, '1110111110111111': 1, '1111110110011111': 4, '0111111110011111': 1, '0111101111111111': 2, '1111101111111101': 4, '1110101110111111': 1, '1101111111111111': 20, '1110101111111111': 2, '1011111111010111': 1, '1111010111111111': 6, '1011111011111111': 1, '1111101110111111': 7, '1111111111001111': 2, '1111110111111111': 55, '1101011101111111': 1, '0111111011111101': 1, '1111011111111110': 1, '1111111011011111': 3, '1110110100111101': 1, '0111111111111101': 1, '0011111111111111': 2, '0111101111110111': 1, '1111111011111111': 25, '1111011111011111': 5, '1011001110111101': 1, '1101101110111111': 1, '1111111110110111': 2, '1011011111011111': 1, '1111110101111111': 3, '1111101111101011': 1, '1111110111110111': 2, '1111101010111111': 2, '1111111110011101': 1, '0111111111011111': 1, '1111101111101111': 1, '1111111111100111': 2, '1101110111111111': 2, '1110111111011111': 1, '1111111111111110': 9, '1111110111111101': 2, '1111011110111111': 6, '1111100110111111': 1, '1111111111011111': 50, '1111100011111111': 2, '1110110111101111': 1, '1111101101011111': 3, '1111101111111011': 1, '1111111110111101': 4, '0101111110101111': 1, '0111111111111111': 7, '1111101111111111': 52, '1111000111111111': 2, '0111101010111111': 1, '1111111110111111': 54, '1111010111011111': 1, '1111011110111101': 1, '1111111111011011': 2, '1111010101111111': 1, '1110101110111011': 1, '1011111101011111': 1, '1111111110111011': 2, '1111011111111111': 11, '1111111111101111': 16, '1111111111111000': 1, '1111111100101111': 1, '1111110111011111': 9, '0111011100111111': 1, '1101101111011111': 1, '1111111101011111': 1, '1101111111011111': 5, '1101111111111011': 2, '1111111111111101': 21, '1101111110111111': 6, '1111110011111111': 4, '1111111001111111': 1, '1011111101111111': 1, '1111111100111111': 3, '1111111101101111': 2, '0111111111111011': 2, '1111001100011111': 1, '1111111110101111': 2, '0111011011111111': 1, '1111111111110111': 5, '1111110111111011': 4, '1111110110111111': 11, '1011111111111111': 20, '1111101100101111': 1, '1111101111011111': 10, '1111100111111111': 6, '1111111101111101': 2, '1111111111111011': 30, '1111111111111111': 354}
Expected number of qubits: 16
Actual bitstring length: 16
Bitstring: 1111111110001111, Index: 65423, Row: 16355, Col: 3, Count: 1
---------------------------------------------------------------------------

IndexError                                Traceback (most recent call last)

Cell In[2], line 87

     83         result_array[row, col] = count

     85     return result_array

---> 87 result_array = counts_to_2d_array(counts, (4, 4))

     89 print("Result array:\n", result_array)  # Debug print

     91 # Plot the initial wave function and the result



Cell In[2], line 83, in counts_to_2d_array(counts, grid_size)

     81     col = index % grid_size[1]

     82     print(f"Bitstring: {truncated_bitstring}, Index: {index}, Row: {row}, Col: {col}, Count: {count}")  # Debug print

---> 83     result_array[row, col] = count

     85 return result_array



IndexError: index 16355 is out of bounds for axis 0 with size 4
[ ]:
import numpy as np
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister, transpile, assemble
from qiskit.visualization import plot_histogram
from qiskit_aer import AerSimulator
import matplotlib.pyplot as plt

def initialize_wavefunction_2d(qc, qubits, wave_function):
    """Initialize the quantum circuit with the given 2D wave function."""
    for i, amplitude in enumerate(wave_function.flatten()):
        qc.ry(2 * np.arccos(amplitude), qubits[i])

def apply_phase_shift_2d(qc, qubits, phase_shifts):
    """Apply phase shift to simulate the weak phase object in 2D."""
    for i, phase in enumerate(phase_shifts.flatten()):
        qc.rz(phase, qubits[i])

def simulate_electron_wave_2d(phase_shifts, grid_size=(4, 4)):
    n_qubits = grid_size[0] * grid_size[1]

    # Initialize quantum circuit
    qr = QuantumRegister(n_qubits, 'q')
    cr = ClassicalRegister(n_qubits, 'c')
    qc = QuantumCircuit(qr, cr)

    # Define initial 2D wave function (Gaussian-like distribution)
    x = np.linspace(-1, 1, grid_size[0])
    y = np.linspace(-1, 1, grid_size[1])
    X, Y = np.meshgrid(x, y)
    initial_wave_function = np.exp(-0.5 * (X**2 + Y**2))
    initial_wave_function /= np.linalg.norm(initial_wave_function)

    print("Initial 2D wave function:\n", initial_wave_function)  # Debug print

    # Initialize wave function in the quantum circuit
    initialize_wavefunction_2d(qc, qr, initial_wave_function)

    # Apply phase shift to simulate the weak phase object
    apply_phase_shift_2d(qc, qr, phase_shifts)

    # Measure the results
    qc.measure(qr, cr)

    print("Quantum circuit:\n", qc)  # Debug print

    return qc, initial_wave_function

# Define phase shifts to simulate the weak phase object in 2D
phase_shifts = np.array([[0.1, 0.2, 0.3, 0.4],
                         [0.2, 0.3, 0.4, 0.5],
                         [0.3, 0.4, 0.5, 0.6],
                         [0.4, 0.5, 0.6, 0.7]])

# Simulate electron wave in 2D
qc, initial_wave_function = simulate_electron_wave_2d(phase_shifts, grid_size=(4, 4))

# Execute the circuit using AerSimulator
simulator = AerSimulator()
compiled_circuit = transpile(qc, simulator)
print("Compiled circuit:\n", compiled_circuit)  # Debug print
sim_result = simulator.run(compiled_circuit).result()
counts = sim_result.get_counts()

print("Counts:", counts)  # Debug print

# Verify the number of qubits
n_qubits = 4 * 4
bitstring_length = len(list(counts.keys())[0])
print(f"Expected number of qubits: {n_qubits}")
print(f"Actual bitstring length: {bitstring_length}")

# Convert counts to 2D array for visualization
def counts_to_2d_array(counts, grid_size):
    n_qubits = grid_size[0] * grid_size[1]
    result_array = np.zeros((grid_size[0], grid_size[1]))

    for bitstring, count in counts.items():
        # Truncate the bitstring to the expected length
        truncated_bitstring = bitstring[-n_qubits:]
        index = int(truncated_bitstring, 2)
        row = index // grid_size[1]
        col = index % grid_size[1]
        print(f"Bitstring: {truncated_bitstring}, Index: {index}, Row: {row}, Col: {col}, Count: {count}")  # Debug print
        result_array[row, col] = count

    return result_array

result_array = counts_to_2d_array(counts, (4, 4))

print("Result array:\n", result_array)  # Debug print

# Plot the initial wave function and the result
fig, ax = plt.subplots(1, 2, figsize=(12, 6))

ax[0].imshow(initial_wave_function, cmap='viridis', origin='lower')
ax[0].set_title("Initial 2D Wave Function")
ax[0].axis('off')

ax[1].imshow(result_array, cmap='viridis', origin='lower')
ax[1].set_title("Electron Wave Simulation Results")
ax[1].axis('off')

plt.show()

Initial 2D wave function:
 [[0.14566959 0.22718971 0.22718971 0.14566959]
 [0.22718971 0.35433041 0.35433041 0.22718971]
 [0.22718971 0.35433041 0.35433041 0.22718971]
 [0.14566959 0.22718971 0.22718971 0.14566959]]
Quantum circuit:
       ┌────────────┐┌─────────┐┌─┐
 q_0: ┤ Ry(2.8492) ├┤ Rz(0.1) ├┤M├─────────────────────────────────────────────
      ├────────────┤├─────────┤└╥┘┌─┐
 q_1: ┤ Ry(2.6832) ├┤ Rz(0.2) ├─╫─┤M├──────────────────────────────────────────
      ├────────────┤├─────────┤ ║ └╥┘┌─┐
 q_2: ┤ Ry(2.6832) ├┤ Rz(0.3) ├─╫──╫─┤M├───────────────────────────────────────
      ├────────────┤├─────────┤ ║  ║ └╥┘┌─┐
 q_3: ┤ Ry(2.8492) ├┤ Rz(0.4) ├─╫──╫──╫─┤M├────────────────────────────────────
      ├────────────┤├─────────┤ ║  ║  ║ └╥┘┌─┐
 q_4: ┤ Ry(2.6832) ├┤ Rz(0.2) ├─╫──╫──╫──╫─┤M├─────────────────────────────────
      ├────────────┤├─────────┤ ║  ║  ║  ║ └╥┘┌─┐
 q_5: ┤ Ry(2.4172) ├┤ Rz(0.3) ├─╫──╫──╫──╫──╫─┤M├──────────────────────────────
      ├────────────┤├─────────┤ ║  ║  ║  ║  ║ └╥┘┌─┐
 q_6: ┤ Ry(2.4172) ├┤ Rz(0.4) ├─╫──╫──╫──╫──╫──╫─┤M├───────────────────────────
      ├────────────┤├─────────┤ ║  ║  ║  ║  ║  ║ └╥┘┌─┐
 q_7: ┤ Ry(2.6832) ├┤ Rz(0.5) ├─╫──╫──╫──╫──╫──╫──╫─┤M├────────────────────────
      ├────────────┤├─────────┤ ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
 q_8: ┤ Ry(2.6832) ├┤ Rz(0.3) ├─╫──╫──╫──╫──╫──╫──╫──╫─┤M├─────────────────────
      ├────────────┤├─────────┤ ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
 q_9: ┤ Ry(2.4172) ├┤ Rz(0.4) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├──────────────────
      ├────────────┤├─────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_10: ┤ Ry(2.4172) ├┤ Rz(0.5) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├───────────────
      ├────────────┤├─────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_11: ┤ Ry(2.6832) ├┤ Rz(0.6) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├────────────
      ├────────────┤├─────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_12: ┤ Ry(2.8492) ├┤ Rz(0.4) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├─────────
      ├────────────┤├─────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_13: ┤ Ry(2.6832) ├┤ Rz(0.5) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├──────
      ├────────────┤├─────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_14: ┤ Ry(2.6832) ├┤ Rz(0.6) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├───
      ├────────────┤├─────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_15: ┤ Ry(2.8492) ├┤ Rz(0.7) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├
      └────────────┘└─────────┘ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘
c: 16/══════════════════════════╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩═
                                0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15
Compiled circuit:
 global phase: 3.0832
      ┌──────────────────┐┌─┐
 q_0: ┤ U3(2.8492,0.1,0) ├┤M├─────────────────────────────────────────────
      ├──────────────────┤└╥┘┌─┐
 q_1: ┤ U3(2.6832,0.2,0) ├─╫─┤M├──────────────────────────────────────────
      ├──────────────────┤ ║ └╥┘┌─┐
 q_2: ┤ U3(2.6832,0.3,0) ├─╫──╫─┤M├───────────────────────────────────────
      ├──────────────────┤ ║  ║ └╥┘┌─┐
 q_3: ┤ U3(2.8492,0.4,0) ├─╫──╫──╫─┤M├────────────────────────────────────
      ├──────────────────┤ ║  ║  ║ └╥┘┌─┐
 q_4: ┤ U3(2.6832,0.2,0) ├─╫──╫──╫──╫─┤M├─────────────────────────────────
      ├──────────────────┤ ║  ║  ║  ║ └╥┘┌─┐
 q_5: ┤ U3(2.4172,0.3,0) ├─╫──╫──╫──╫──╫─┤M├──────────────────────────────
      ├──────────────────┤ ║  ║  ║  ║  ║ └╥┘┌─┐
 q_6: ┤ U3(2.4172,0.4,0) ├─╫──╫──╫──╫──╫──╫─┤M├───────────────────────────
      ├──────────────────┤ ║  ║  ║  ║  ║  ║ └╥┘┌─┐
 q_7: ┤ U3(2.6832,0.5,0) ├─╫──╫──╫──╫──╫──╫──╫─┤M├────────────────────────
      ├──────────────────┤ ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
 q_8: ┤ U3(2.6832,0.3,0) ├─╫──╫──╫──╫──╫──╫──╫──╫─┤M├─────────────────────
      ├──────────────────┤ ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
 q_9: ┤ U3(2.4172,0.4,0) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├──────────────────
      ├──────────────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_10: ┤ U3(2.4172,0.5,0) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├───────────────
      ├──────────────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_11: ┤ U3(2.6832,0.6,0) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├────────────
      ├──────────────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_12: ┤ U3(2.8492,0.4,0) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├─────────
      ├──────────────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_13: ┤ U3(2.6832,0.5,0) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├──────
      ├──────────────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_14: ┤ U3(2.6832,0.6,0) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├───
      ├──────────────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_15: ┤ U3(2.8492,0.7,0) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├
      └──────────────────┘ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘
c: 16/═════════════════════╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩═
                           0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15
Counts: {'1111111110001111': 1, '1110111111110111': 1, '1011111111110011': 1, '1111110111100111': 1, '1111111010011111': 1, '1111110010111011': 1, '1011101110111111': 1, '1111001101111111': 1, '1101101111111111': 1, '1101111111101111': 1, '1111111110110011': 1, '1011111010111111': 1, '1111110101111011': 1, '1111100111011111': 1, '1011101111111011': 2, '1010111111011111': 1, '1111101001101111': 1, '1111111000111111': 1, '1111110100111111': 1, '1011101111111111': 2, '1101011111011111': 1, '1111101011111111': 1, '1101111110111101': 1, '1111101111111110': 1, '1011111111110111': 1, '1111110010111111': 1, '0111111110111111': 1, '1110111100111111': 1, '1101110110111111': 1, '1111110111011101': 1, '1011101111011111': 2, '1111011101111111': 1, '1111101101111111': 1, '1111110111011011': 1, '1111101101111101': 1, '1101111111111110': 2, '1111110111101111': 3, '1011100100111111': 1, '1111001111011111': 1, '1111101101111011': 1, '1111101111110111': 2, '1011110110111111': 1, '1110111111111111': 12, '0110111111111111': 1, '1111111111011110': 1, '1011111111111101': 1, '1111001110111111': 1, '0111101110111111': 1, '1111111011111011': 1, '1111111110011111': 6, '1111111110010111': 1, '1111111101101101': 1, '1111011111111011': 1, '1111110111111110': 1, '1111111111011101': 5, '1111111101111011': 1, '1011110111111111': 5, '1101100111111111': 1, '1101111001111111': 1, '1111110110111011': 1, '1111100110111011': 1, '1101011111111111': 2, '1111111101111111': 8, '1011110111011111': 1, '1110110111011111': 1, '1111111010111111': 3, '1110101111011111': 1, '1111011111101111': 2, '1011111110111111': 1, '1011111111011111': 2, '1011011111111101': 1, '1111111101110111': 1, '1110111110111111': 1, '1111110110011111': 4, '0111111110011111': 1, '0111101111111111': 2, '1111101111111101': 4, '1110101110111111': 1, '1101111111111111': 20, '1110101111111111': 2, '1011111111010111': 1, '1111010111111111': 6, '1011111011111111': 1, '1111101110111111': 7, '1111111111001111': 2, '1111110111111111': 55, '1101011101111111': 1, '0111111011111101': 1, '1111011111111110': 1, '1111111011011111': 3, '1110110100111101': 1, '0111111111111101': 1, '0011111111111111': 2, '0111101111110111': 1, '1111111011111111': 25, '1111011111011111': 5, '1011001110111101': 1, '1101101110111111': 1, '1111111110110111': 2, '1011011111011111': 1, '1111110101111111': 3, '1111101111101011': 1, '1111110111110111': 2, '1111101010111111': 2, '1111111110011101': 1, '0111111111011111': 1, '1111101111101111': 1, '1111111111100111': 2, '1101110111111111': 2, '1110111111011111': 1, '1111111111111110': 9, '1111110111111101': 2, '1111011110111111': 6, '1111100110111111': 1, '1111111111011111': 50, '1111100011111111': 2, '1110110111101111': 1, '1111101101011111': 3, '1111101111111011': 1, '1111111110111101': 4, '0101111110101111': 1, '0111111111111111': 7, '1111101111111111': 52, '1111000111111111': 2, '0111101010111111': 1, '1111111110111111': 54, '1111010111011111': 1, '1111011110111101': 1, '1111111111011011': 2, '1111010101111111': 1, '1110101110111011': 1, '1011111101011111': 1, '1111111110111011': 2, '1111011111111111': 11, '1111111111101111': 16, '1111111111111000': 1, '1111111100101111': 1, '1111110111011111': 9, '0111011100111111': 1, '1101101111011111': 1, '1111111101011111': 1, '1101111111011111': 5, '1101111111111011': 2, '1111111111111101': 21, '1101111110111111': 6, '1111110011111111': 4, '1111111001111111': 1, '1011111101111111': 1, '1111111100111111': 3, '1111111101101111': 2, '0111111111111011': 2, '1111001100011111': 1, '1111111110101111': 2, '0111011011111111': 1, '1111111111110111': 5, '1111110111111011': 4, '1111110110111111': 11, '1011111111111111': 20, '1111101100101111': 1, '1111101111011111': 10, '1111100111111111': 6, '1111111101111101': 2, '1111111111111011': 30, '1111111111111111': 354}
Expected number of qubits: 16
Actual bitstring length: 16
Bitstring: 1111111110001111, Index: 65423, Row: 16355, Col: 3, Count: 1
---------------------------------------------------------------------------

IndexError                                Traceback (most recent call last)

Cell In[2], line 87

     83         result_array[row, col] = count

     85     return result_array

---> 87 result_array = counts_to_2d_array(counts, (4, 4))

     89 print("Result array:\n", result_array)  # Debug print

     91 # Plot the initial wave function and the result



Cell In[2], line 83, in counts_to_2d_array(counts, grid_size)

     81     col = index % grid_size[1]

     82     print(f"Bitstring: {truncated_bitstring}, Index: {index}, Row: {row}, Col: {col}, Count: {count}")  # Debug print

---> 83     result_array[row, col] = count

     85 return result_array



IndexError: index 16355 is out of bounds for axis 0 with size 4
[ ]:
import numpy as np
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister, transpile, assemble
from qiskit.visualization import plot_histogram
from qiskit_aer import AerSimulator
import matplotlib.pyplot as plt

def initialize_wavefunction_2d(qc, qubits, wave_function):
    """Initialize the quantum circuit with the given 2D wave function."""
    for i, amplitude in enumerate(wave_function.flatten()):
        qc.ry(2 * np.arccos(amplitude), qubits[i])

def apply_phase_shift_2d(qc, qubits, phase_shifts):
    """Apply phase shift to simulate the weak phase object in 2D."""
    for i, phase in enumerate(phase_shifts.flatten()):
        qc.rz(phase, qubits[i])

def simulate_electron_wave_2d(phase_shifts, grid_size=(4, 4)):
    n_qubits = grid_size[0] * grid_size[1]

    # Initialize quantum circuit
    qr = QuantumRegister(n_qubits, 'q')
    cr = ClassicalRegister(n_qubits, 'c')
    qc = QuantumCircuit(qr, cr)

    # Define initial 2D wave function (Gaussian-like distribution)
    x = np.linspace(-1, 1, grid_size[0])
    y = np.linspace(-1, 1, grid_size[1])
    X, Y = np.meshgrid(x, y)
    initial_wave_function = np.exp(-0.5 * (X**2 + Y**2))
    initial_wave_function /= np.linalg.norm(initial_wave_function)

    print("Initial 2D wave function:\n", initial_wave_function)  # Debug print

    # Initialize wave function in the quantum circuit
    initialize_wavefunction_2d(qc, qr, initial_wave_function)

    # Apply phase shift to simulate the weak phase object
    apply_phase_shift_2d(qc, qr, phase_shifts)

    # Measure the results
    qc.measure(qr, cr)

    print("Quantum circuit:\n", qc)  # Debug print

    return qc, initial_wave_function

# Define phase shifts to simulate the weak phase object in 2D
phase_shifts = np.array([[0.1, 0.2, 0.3, 0.4],
                         [0.2, 0.3, 0.4, 0.5],
                         [0.3, 0.4, 0.5, 0.6],
                         [0.4, 0.5, 0.6, 0.7]])

# Simulate electron wave in 2D
qc, initial_wave_function = simulate_electron_wave_2d(phase_shifts, grid_size=(4, 4))

# Execute the circuit using AerSimulator
simulator = AerSimulator()
compiled_circuit = transpile(qc, simulator)
print("Compiled circuit:\n", compiled_circuit)  # Debug print
sim_result = simulator.run(compiled_circuit).result()
counts = sim_result.get_counts()

print("Counts:", counts)  # Debug print

# Verify the number of qubits
n_qubits = 4 * 4
bitstring_length = len(list(counts.keys())[0])
print(f"Expected number of qubits: {n_qubits}")
print(f"Actual bitstring length: {bitstring_length}")

# Convert counts to 2D array for visualization
def counts_to_2d_array(counts, grid_size):
    n_qubits = grid_size[0] * grid_size[1]
    result_array = np.zeros((grid_size[0], grid_size[1]))

    for bitstring, count in counts.items():
        # Truncate the bitstring to the expected length
        truncated_bitstring = bitstring[-n_qubits:]
        index = int(truncated_bitstring, 2)
        row = index // grid_size[1]
        col = index % grid_size[1]
        print(f"Bitstring: {truncated_bitstring}, Index: {index}, Row: {row}, Col: {col}, Count: {count}")  # Debug print
        result_array[row, col] = count

    return result_array

result_array = counts_to_2d_array(counts, (4, 4))

print("Result array:\n", result_array)  # Debug print

# Plot the initial wave function and the result
fig, ax = plt.subplots(1, 2, figsize=(12, 6))

ax[0].imshow(initial_wave_function, cmap='viridis', origin='lower')
ax[0].set_title("Initial 2D Wave Function")
ax[0].axis('off')

ax[1].imshow(result_array, cmap='viridis', origin='lower')
ax[1].set_title("Electron Wave Simulation Results")
ax[1].axis('off')

plt.show()

Initial 2D wave function:
 [[0.14566959 0.22718971 0.22718971 0.14566959]
 [0.22718971 0.35433041 0.35433041 0.22718971]
 [0.22718971 0.35433041 0.35433041 0.22718971]
 [0.14566959 0.22718971 0.22718971 0.14566959]]
Quantum circuit:
       ┌────────────┐┌─────────┐┌─┐
 q_0: ┤ Ry(2.8492) ├┤ Rz(0.1) ├┤M├─────────────────────────────────────────────
      ├────────────┤├─────────┤└╥┘┌─┐
 q_1: ┤ Ry(2.6832) ├┤ Rz(0.2) ├─╫─┤M├──────────────────────────────────────────
      ├────────────┤├─────────┤ ║ └╥┘┌─┐
 q_2: ┤ Ry(2.6832) ├┤ Rz(0.3) ├─╫──╫─┤M├───────────────────────────────────────
      ├────────────┤├─────────┤ ║  ║ └╥┘┌─┐
 q_3: ┤ Ry(2.8492) ├┤ Rz(0.4) ├─╫──╫──╫─┤M├────────────────────────────────────
      ├────────────┤├─────────┤ ║  ║  ║ └╥┘┌─┐
 q_4: ┤ Ry(2.6832) ├┤ Rz(0.2) ├─╫──╫──╫──╫─┤M├─────────────────────────────────
      ├────────────┤├─────────┤ ║  ║  ║  ║ └╥┘┌─┐
 q_5: ┤ Ry(2.4172) ├┤ Rz(0.3) ├─╫──╫──╫──╫──╫─┤M├──────────────────────────────
      ├────────────┤├─────────┤ ║  ║  ║  ║  ║ └╥┘┌─┐
 q_6: ┤ Ry(2.4172) ├┤ Rz(0.4) ├─╫──╫──╫──╫──╫──╫─┤M├───────────────────────────
      ├────────────┤├─────────┤ ║  ║  ║  ║  ║  ║ └╥┘┌─┐
 q_7: ┤ Ry(2.6832) ├┤ Rz(0.5) ├─╫──╫──╫──╫──╫──╫──╫─┤M├────────────────────────
      ├────────────┤├─────────┤ ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
 q_8: ┤ Ry(2.6832) ├┤ Rz(0.3) ├─╫──╫──╫──╫──╫──╫──╫──╫─┤M├─────────────────────
      ├────────────┤├─────────┤ ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
 q_9: ┤ Ry(2.4172) ├┤ Rz(0.4) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├──────────────────
      ├────────────┤├─────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_10: ┤ Ry(2.4172) ├┤ Rz(0.5) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├───────────────
      ├────────────┤├─────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_11: ┤ Ry(2.6832) ├┤ Rz(0.6) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├────────────
      ├────────────┤├─────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_12: ┤ Ry(2.8492) ├┤ Rz(0.4) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├─────────
      ├────────────┤├─────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_13: ┤ Ry(2.6832) ├┤ Rz(0.5) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├──────
      ├────────────┤├─────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_14: ┤ Ry(2.6832) ├┤ Rz(0.6) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├───
      ├────────────┤├─────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_15: ┤ Ry(2.8492) ├┤ Rz(0.7) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├
      └────────────┘└─────────┘ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘
c: 16/══════════════════════════╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩═
                                0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15
Compiled circuit:
 global phase: 3.0832
      ┌──────────────────┐┌─┐
 q_0: ┤ U3(2.8492,0.1,0) ├┤M├─────────────────────────────────────────────
      ├──────────────────┤└╥┘┌─┐
 q_1: ┤ U3(2.6832,0.2,0) ├─╫─┤M├──────────────────────────────────────────
      ├──────────────────┤ ║ └╥┘┌─┐
 q_2: ┤ U3(2.6832,0.3,0) ├─╫──╫─┤M├───────────────────────────────────────
      ├──────────────────┤ ║  ║ └╥┘┌─┐
 q_3: ┤ U3(2.8492,0.4,0) ├─╫──╫──╫─┤M├────────────────────────────────────
      ├──────────────────┤ ║  ║  ║ └╥┘┌─┐
 q_4: ┤ U3(2.6832,0.2,0) ├─╫──╫──╫──╫─┤M├─────────────────────────────────
      ├──────────────────┤ ║  ║  ║  ║ └╥┘┌─┐
 q_5: ┤ U3(2.4172,0.3,0) ├─╫──╫──╫──╫──╫─┤M├──────────────────────────────
      ├──────────────────┤ ║  ║  ║  ║  ║ └╥┘┌─┐
 q_6: ┤ U3(2.4172,0.4,0) ├─╫──╫──╫──╫──╫──╫─┤M├───────────────────────────
      ├──────────────────┤ ║  ║  ║  ║  ║  ║ └╥┘┌─┐
 q_7: ┤ U3(2.6832,0.5,0) ├─╫──╫──╫──╫──╫──╫──╫─┤M├────────────────────────
      ├──────────────────┤ ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
 q_8: ┤ U3(2.6832,0.3,0) ├─╫──╫──╫──╫──╫──╫──╫──╫─┤M├─────────────────────
      ├──────────────────┤ ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
 q_9: ┤ U3(2.4172,0.4,0) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├──────────────────
      ├──────────────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_10: ┤ U3(2.4172,0.5,0) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├───────────────
      ├──────────────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_11: ┤ U3(2.6832,0.6,0) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├────────────
      ├──────────────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_12: ┤ U3(2.8492,0.4,0) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├─────────
      ├──────────────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_13: ┤ U3(2.6832,0.5,0) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├──────
      ├──────────────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_14: ┤ U3(2.6832,0.6,0) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├───
      ├──────────────────┤ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_15: ┤ U3(2.8492,0.7,0) ├─╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├
      └──────────────────┘ ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘
c: 16/═════════════════════╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩═
                           0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15
Counts: {'1111111110001111': 1, '1110111111110111': 1, '1011111111110011': 1, '1111110111100111': 1, '1111111010011111': 1, '1111110010111011': 1, '1011101110111111': 1, '1111001101111111': 1, '1101101111111111': 1, '1101111111101111': 1, '1111111110110011': 1, '1011111010111111': 1, '1111110101111011': 1, '1111100111011111': 1, '1011101111111011': 2, '1010111111011111': 1, '1111101001101111': 1, '1111111000111111': 1, '1111110100111111': 1, '1011101111111111': 2, '1101011111011111': 1, '1111101011111111': 1, '1101111110111101': 1, '1111101111111110': 1, '1011111111110111': 1, '1111110010111111': 1, '0111111110111111': 1, '1110111100111111': 1, '1101110110111111': 1, '1111110111011101': 1, '1011101111011111': 2, '1111011101111111': 1, '1111101101111111': 1, '1111110111011011': 1, '1111101101111101': 1, '1101111111111110': 2, '1111110111101111': 3, '1011100100111111': 1, '1111001111011111': 1, '1111101101111011': 1, '1111101111110111': 2, '1011110110111111': 1, '1110111111111111': 12, '0110111111111111': 1, '1111111111011110': 1, '1011111111111101': 1, '1111001110111111': 1, '0111101110111111': 1, '1111111011111011': 1, '1111111110011111': 6, '1111111110010111': 1, '1111111101101101': 1, '1111011111111011': 1, '1111110111111110': 1, '1111111111011101': 5, '1111111101111011': 1, '1011110111111111': 5, '1101100111111111': 1, '1101111001111111': 1, '1111110110111011': 1, '1111100110111011': 1, '1101011111111111': 2, '1111111101111111': 8, '1011110111011111': 1, '1110110111011111': 1, '1111111010111111': 3, '1110101111011111': 1, '1111011111101111': 2, '1011111110111111': 1, '1011111111011111': 2, '1011011111111101': 1, '1111111101110111': 1, '1110111110111111': 1, '1111110110011111': 4, '0111111110011111': 1, '0111101111111111': 2, '1111101111111101': 4, '1110101110111111': 1, '1101111111111111': 20, '1110101111111111': 2, '1011111111010111': 1, '1111010111111111': 6, '1011111011111111': 1, '1111101110111111': 7, '1111111111001111': 2, '1111110111111111': 55, '1101011101111111': 1, '0111111011111101': 1, '1111011111111110': 1, '1111111011011111': 3, '1110110100111101': 1, '0111111111111101': 1, '0011111111111111': 2, '0111101111110111': 1, '1111111011111111': 25, '1111011111011111': 5, '1011001110111101': 1, '1101101110111111': 1, '1111111110110111': 2, '1011011111011111': 1, '1111110101111111': 3, '1111101111101011': 1, '1111110111110111': 2, '1111101010111111': 2, '1111111110011101': 1, '0111111111011111': 1, '1111101111101111': 1, '1111111111100111': 2, '1101110111111111': 2, '1110111111011111': 1, '1111111111111110': 9, '1111110111111101': 2, '1111011110111111': 6, '1111100110111111': 1, '1111111111011111': 50, '1111100011111111': 2, '1110110111101111': 1, '1111101101011111': 3, '1111101111111011': 1, '1111111110111101': 4, '0101111110101111': 1, '0111111111111111': 7, '1111101111111111': 52, '1111000111111111': 2, '0111101010111111': 1, '1111111110111111': 54, '1111010111011111': 1, '1111011110111101': 1, '1111111111011011': 2, '1111010101111111': 1, '1110101110111011': 1, '1011111101011111': 1, '1111111110111011': 2, '1111011111111111': 11, '1111111111101111': 16, '1111111111111000': 1, '1111111100101111': 1, '1111110111011111': 9, '0111011100111111': 1, '1101101111011111': 1, '1111111101011111': 1, '1101111111011111': 5, '1101111111111011': 2, '1111111111111101': 21, '1101111110111111': 6, '1111110011111111': 4, '1111111001111111': 1, '1011111101111111': 1, '1111111100111111': 3, '1111111101101111': 2, '0111111111111011': 2, '1111001100011111': 1, '1111111110101111': 2, '0111011011111111': 1, '1111111111110111': 5, '1111110111111011': 4, '1111110110111111': 11, '1011111111111111': 20, '1111101100101111': 1, '1111101111011111': 10, '1111100111111111': 6, '1111111101111101': 2, '1111111111111011': 30, '1111111111111111': 354}
Expected number of qubits: 16
Actual bitstring length: 16
Bitstring: 1111111110001111, Index: 65423, Row: 16355, Col: 3, Count: 1
---------------------------------------------------------------------------

IndexError                                Traceback (most recent call last)

Cell In[2], line 87

     83         result_array[row, col] = count

     85     return result_array

---> 87 result_array = counts_to_2d_array(counts, (4, 4))

     89 print("Result array:\n", result_array)  # Debug print

     91 # Plot the initial wave function and the result



Cell In[2], line 83, in counts_to_2d_array(counts, grid_size)

     81     col = index % grid_size[1]

     82     print(f"Bitstring: {truncated_bitstring}, Index: {index}, Row: {row}, Col: {col}, Count: {count}")  # Debug print

---> 83     result_array[row, col] = count

     85 return result_array



IndexError: index 16355 is out of bounds for axis 0 with size 4
[ ]: