Skip to content

Commit 0fe095d

Browse files
committed
added codes on sensing as examples, still some small errors
1 parent c789bd8 commit 0fe095d

File tree

2 files changed

+100
-34
lines changed

2 files changed

+100
-34
lines changed

doc/Programs/Sensing/twoq.py

Lines changed: 53 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,66 @@
11
import numpy as np
2-
from qiskit import QuantumCircuit, execute, Aer
2+
import matplotlib.pyplot as plt
33

4-
# Define the number of qubits
5-
num_qubits = 2
4+
# Define parameters
5+
B = 1.0 # Strength of the magnetic field (in arbitrary units)
6+
omega = B # Frequency associated with the magnetic field
67

7-
# Create a quantum circuit with the specified number of qubits
8-
qc = QuantumCircuit(num_qubits)
8+
# Create basis states |00>, |01>, |10>, |11>
9+
basis_00 = np.array([1, 0, 0, 0]) # |00>
10+
basis_01 = np.array([0, 1, 0, 0]) # |01>
11+
basis_10 = np.array([0, 0, 1, 0]) # |10>
12+
basis_11 = np.array([0, 0, 0, 1]) # |11>
913

10-
# Apply a Hadamard gate to the first qubit to create a superposition
11-
qc.h(0)
14+
# Create an entangled state (Bell state) |Φ+> = (|00> + |11>) / sqrt(2)
15+
entangled_state = (basis_00 + basis_11) / np.sqrt(2)
1216

13-
# Apply a CNOT gate to entangle the two qubits
14-
qc.cx(0, 1)
17+
# Define Pauli matrices
18+
sigma_z = np.array([[1, 0], [0, -1]])
19+
identity = np.eye(2)
1520

16-
# Apply a time-dependent field to the system
17-
time = np.linspace(0, 10, 101)
18-
field_strength = 0.1 * np.sin(2 * np.pi * time)
21+
# Constructing the Hamiltonian H = -ω/2 * (σz ⊗ I + I ⊗ σz)
22+
H_z_I = -omega / 2 * np.kron(sigma_z, identity) # σz ⊗ I
23+
I_H_z = -omega / 2 * np.kron(identity, sigma_z) # I ⊗ σz
24+
H = H_z_I + I_H_z # Total Hamiltonian
1925

20-
for t, B in zip(time, field_strength):
21-
# Apply a rotation around the Z-axis with the time-dependent field strength
22-
qc.rz(B, 1)
23-
qc.barrier()
26+
# Time evolution parameters
27+
t_list = np.linspace(0, 10, num=100) # Time from t=0 to t=10
28+
dt = t_list[1] - t_list[0]
2429

25-
# Measure the qubits
26-
qc.measure_all()
30+
def evolve(state):
31+
"""Evolve quantum state under Hamiltonian."""
32+
return np.dot(expm(-1j * H * dt), state)
2733

28-
# Execute the circuit on a simulator
29-
backend = Aer.get_backend('qasm_simulator')
30-
job = execute(qc, backend, shots=1024)
31-
result = job.result()
34+
# Evolve the initial state over time
35+
result_states = []
36+
current_state = entangled_state.copy()
3237

33-
# Get the measurement counts
34-
counts = result.get_counts(qc)
38+
for _ in t_list:
39+
result_states.append(current_state)
40+
current_state = evolve(current_state)
3541

36-
# Print the measurement results
37-
print("Measurement results:")
38-
for state, count in counts.items():
39-
print(f"{state}: {count}")
40-
"""
41-
5. We apply a CNOT gate to entangle the two qubits.
42-
6. We apply a time-dependent field to the system by rotating the second qubit around the Z-axis with a field strength that varies sinusoidally over time.
43-
7. We measure the qubits and execute the circuit on a simulator.
44-
8. We print the measurement results, showing the counts for each possible measurement outcome.
42+
# Calculate probabilities of measuring specific states: P(|00>) and P(|11>)
43+
probabilities_00 = [abs(np.dot(basis_00.conj(), state))**2 for state in result_states]
44+
probabilities_11 = [abs(np.dot(basis_11.conj(), state))**2 for state in result_states]
45+
46+
# Output results
47+
print("Probabilities of measuring |00>: ", probabilities_00)
48+
print("Probabilities of measuring |11>: ", probabilities_11)
4549

46-
This code can be used as a starting point for quantum sensing applications, where the time-dependent field can be used to probe the environment and detect changes or anomalies. The entanglement between the qubits can enhance the sensitivity of the measurement, making it a powerful tool for various quantum sensing applications.
50+
# Plotting results using matplotlib
51+
plt.plot(t_list, probabilities_00,label='Probability of |00>')
52+
plt.plot(t_list, probabilities_11,label='Probability of |11>')
53+
plt.xlabel('Time')
54+
plt.ylabel('Probability')
55+
plt.title('Quantum Sensing Simulation')
56+
plt.legend()
57+
plt.show()
58+
59+
"""
60+
### Explanation:
61+
- **Basis States:** We define four basis states corresponding to two qubits.
62+
- **Entanglement Creation:** The Bell state \(|\Phi^+\rangle\) is created manually.
63+
- **Hamiltonian Definition:** The Hamiltonian \(H\) combines contributions from both qubits affected by a static magnetic field.
64+
- **State Evolution:** A function `evolve` computes the new quantum state at each time step using matrix exponentiation to apply the unitary operator derived from the Hamiltonian.
65+
- **Measurement Probabilities:** Finally we compute measurement probabilities for \(|00\rangle\) and \(|11\rangle\).
4766
"""
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import numpy as np
2+
from qiskit import QuantumCircuit, execute, Aer
3+
4+
# Define the number of qubits
5+
num_qubits = 2
6+
7+
# Create a quantum circuit with the specified number of qubits
8+
qc = QuantumCircuit(num_qubits)
9+
10+
# Apply a Hadamard gate to the first qubit to create a superposition
11+
qc.h(0)
12+
13+
# Apply a CNOT gate to entangle the two qubits
14+
qc.cx(0, 1)
15+
16+
# Apply a time-dependent field to the system
17+
time = np.linspace(0, 10, 101)
18+
field_strength = 0.1 * np.sin(2 * np.pi * time)
19+
20+
for t, B in zip(time, field_strength):
21+
# Apply a rotation around the Z-axis with the time-dependent field strength
22+
qc.rz(B, 1)
23+
qc.barrier()
24+
25+
# Measure the qubits
26+
qc.measure_all()
27+
28+
# Execute the circuit on a simulator
29+
backend = Aer.get_backend('qasm_simulator')
30+
job = execute(qc, backend, shots=1024)
31+
result = job.result()
32+
33+
# Get the measurement counts
34+
counts = result.get_counts(qc)
35+
36+
# Print the measurement results
37+
print("Measurement results:")
38+
for state, count in counts.items():
39+
print(f"{state}: {count}")
40+
"""
41+
5. We apply a CNOT gate to entangle the two qubits.
42+
6. We apply a time-dependent field to the system by rotating the second qubit around the Z-axis with a field strength that varies sinusoidally over time.
43+
7. We measure the qubits and execute the circuit on a simulator.
44+
8. We print the measurement results, showing the counts for each possible measurement outcome.
45+
46+
This code can be used as a starting point for quantum sensing applications, where the time-dependent field can be used to probe the environment and detect changes or anomalies. The entanglement between the qubits can enhance the sensitivity of the measurement, making it a powerful tool for various quantum sensing applications.
47+
"""

0 commit comments

Comments
 (0)