|
| 1 | +import numpy as np |
| 2 | +from scipy.linalg import expm |
| 3 | +import matplotlib.pyplot as plt |
| 4 | + |
| 5 | +# Define single-qubit operations, Identity, Pauli, Hadamard and S matrices |
| 6 | +Id = np.array([[1, 0], [0, 1]], dtype=complex) |
| 7 | +X = np.array([[0, 1], [1, 0]], dtype=complex) |
| 8 | +Y = np.array([[0, -1j], [1j, 0]], dtype=complex) |
| 9 | +Z = np.array([[1, 0], [0, -1]], dtype=complex) |
| 10 | +Had = np.array([[1, 1],[1, -1]], dtype=complex) / np.sqrt(2) |
| 11 | +S = np.array([[1, 0],[0, 1j]], dtype=complex) |
| 12 | +# Define two-qubit gates |
| 13 | +CNOT01 = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]], dtype=complex) |
| 14 | +CNOT10 = np.array([[1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0], [0, 1, 0, 0]], dtype=complex) |
| 15 | +SWAP = np.array([[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]], dtype=complex) |
| 16 | + |
| 17 | +times = np.linspace(0, 1, 100) # Time range from 0 to 1 seconds |
| 18 | + |
| 19 | +# Initial state for each qubit |
| 20 | +psi_1 = np.array([1, 0], dtype=complex) # start with |0> for qubits 1 and 2 |
| 21 | +psi_2 = np.array([1, 0], dtype=complex) # start with |0> for qubits 1 and 2 |
| 22 | +# possible basis states for measurements |
| 23 | +basis_00 = np.array([1, 0, 0, 0], dtype=complex) |
| 24 | +basis_01 = np.array([0, 1, 0, 0], dtype=complex) |
| 25 | +basis_10 = np.array([0, 0, 1, 0], dtype=complex) |
| 26 | +basis_11 = np.array([0, 0, 0, 1], dtype=complex) |
| 27 | +# then act with Hadamard on first qubit only |
| 28 | +psi_1 = Had @ psi_1 |
| 29 | +# Initial two-qubit state |
| 30 | +Psi_0 = np.kron(psi_1,psi_2) |
| 31 | +# Define parameters |
| 32 | +# Then we act on this state in order to get a Bell state 1/sqrt(2)(|00>+|11) |
| 33 | +Psi_0 = CNOT01 @ Psi_0 |
| 34 | +B = 1.0 # Strength of the magnetic field (in arbitrary units) |
| 35 | +omega = B # Frequency associated with the magnetic field |
| 36 | +# Constructing the Hamiltonian H = -omega/2 * (Z * I + I * Z) |
| 37 | +H_z_I = -omega / 2 * np.kron(Z, Id) # Z * I |
| 38 | +I_H_z = -omega / 2 * np.kron(Id, Z) # I * Z |
| 39 | +# Total Hamiltonian, try a more complicated one |
| 40 | +H = H_z_I + I_H_z |
| 41 | +# Lists to store expectation values |
| 42 | +expect_00, expect_11 = [], [] |
| 43 | +for t in times: |
| 44 | + # Calculate time evolution operator |
| 45 | + U = expm(-1j * H * t) |
| 46 | + # Evolve the initial state |
| 47 | + Psi_t = U @ Psi_0 |
| 48 | + # Calculate probabilities of measuring specific states: P(|00>) and P(|11>) |
| 49 | + # Calculate expectation values |
| 50 | + expect_00.append(abs(np.dot(basis_00.conj(), Psi_t))**2) |
| 51 | + expect_11.append(abs(np.dot(basis_11.conj(), Psi_t))**2) |
| 52 | + |
| 53 | +# Plotting results using matplotlib |
| 54 | +plt.figure(figsize=(10, 6)) |
| 55 | +plt.plot(times, expect_00, label='Probability |00>') |
| 56 | +plt.plot(times, expect_11, label='Probability |11>') |
| 57 | +plt.xlabel('Time') |
| 58 | +plt.ylabel('Probability') |
| 59 | +plt.title('Quantum Sensing Simulation') |
| 60 | +plt.legend() |
| 61 | +plt.show() |
0 commit comments