Skip to content

Commit

Permalink
Create variational_circuit.py
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Jan 19, 2025
1 parent 6d17d31 commit 4c2ed76
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions quantum/circuits/variational_circuit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# variational_circuit.py
import numpy as np
from qiskit import QuantumCircuit, Aer, execute
from qiskit.visualization import plot_histogram
from qiskit.quantum_info import Statevector
from qiskit.algorithms import VQE
from qiskit.algorithms.optimizers import SLSQP
from qiskit.primitives import Sampler

def create_variational_circuit(num_qubits, params):
"""
Create a parameterized variational circuit.
Parameters:
- num_qubits: Number of qubits in the circuit
- params: List of parameters for the variational gates
Returns:
- QuantumCircuit: The constructed variational circuit
"""
circuit = QuantumCircuit(num_qubits)

# Apply parameterized RY gates
for i in range(num_qubits):
circuit.ry(params[i], i)

# Add entangling gates (CNOTs)
for i in range(num_qubits - 1):
circuit.cx(i, i + 1)

return circuit

def run_variational_algorithm(num_qubits, initial_params):
"""
Run a variational algorithm (e.g., VQE) using the variational circuit.
Parameters:
- num_qubits: Number of qubits in the circuit
- initial_params: Initial parameters for the variational circuit
Returns:
- optimal_value: The minimum eigenvalue found
- optimal_params: The optimal parameters for the variational circuit
"""
# Create a variational circuit
circuit = create_variational_circuit(num_qubits, initial_params)

# Define the optimizer
optimizer = SLSQP(maxiter=100)

# Create a VQE instance
vqe = VQE(circuit, optimizer=optimizer, quantum_instance=Aer.get_backend('aer_simulator'))

# Run VQE
result = vqe.compute_minimum_eigenvalue()

return result.eigenvalue, result.optimal_point

def visualize_results(optimal_value, optimal_params):
"""
Visualize the results of the variational algorithm.
Parameters:
- optimal_value: The minimum eigenvalue found
- optimal_params: The optimal parameters for the variational circuit
"""
print("Minimum Eigenvalue:", optimal_value)
print("Optimal Parameters:", optimal_params)

if __name__ == "__main__":
num_qubits = 2 # Number of qubits for the variational circuit
initial_params = np.random.rand(num_qubits) * np.pi # Random initial parameters

# Run the variational algorithm
optimal_value, optimal_params = run_variational_algorithm(num_qubits, initial_params)

# Visualize the results
visualize_results(optimal_value, optimal_params)

0 comments on commit 4c2ed76

Please sign in to comment.