I want to analyze this code in AWS BRAKET. How can I run this code in AWS BRAKET SV1? How do I create circuit for this problem? How do I initiliaze the circuit and device?
def quantum_algorithm(x_train, y_train, x_test, y_test):
print("Quantum algorithm is started")
print("Finding features")
num_features = x_train.shape[1]
# Drawing circuit diagram
feature_map = ZZFeatureMap(feature_dimension=num_features, reps=1)
feature_map.decompose().draw(output="mpl", style="clifford", fold=20)
print("Drawing ansatz circuit")
ansatz = RealAmplitudes(num_qubits=num_features, reps=3)
ansatz.decompose().draw(output="mpl", style="clifford", fold=20)
optimizer = COBYLA(maxiter=100)
sampler = Sampler()
objective_func_vals = []
plt.rcParams["figure.figsize"] = (12, 6)
def callback_graph(weights, obj_func_eval):
clear_output(wait=True)
objective_func_vals.append(obj_func_eval)
plt.title("Objective function value against iteration")
plt.xlabel("Iteration")
plt.ylabel("Objective function value")
plt.plot(range(len(objective_func_vals)), objective_func_vals)
plt.show()
vqc = VQC(sampler=sampler, feature_map=feature_map, ansatz=ansatz, optimizer=optimizer, callback=callback_graph)
# Clear objective value history
objective_func_vals = []
start = time.time()
print("Learning is started")
vqc.fit(x_train, y_train)
elapsed = time.time() - start
print(f"Training time: {round(elapsed)} seconds")
# Predict on train and test datasets
y_train_pred = vqc.predict(x_train)
y_test_pred = vqc.predict(x_test)
The recommended way is to use the qiskit-braket provider:
- Repo: https://github.com/qiskit-community/qiskit-braket-provider
- Blog post: https://aws.amazon.com/blogs/quantum-computing/introducing-the-qiskit-provider-for-amazon-braket/
- Example run of VQE: https://github.com/qiskit-community/qiskit-braket-provider/blob/main/docs/tutorials/1_tutorial_vqe.ipynb
It allows specifying the Amazon Braket SV1 backend as
from qiskit_braket_provider import AWSBraketProvider
provider = AWSBraketProvider()
braket_sv1 = provider.get_backend("SV1")
For testing purposes, I’d recommend starting with the local simulator backend first
from qiskit_braket_provider import BraketLocalBackend
braket_local_simulator = BraketLocalBackend()
The documentation for VQC (https://docs.quantum.ibm.com/api/qiskit/0.31/qiskit.aqua.algorithms.VQC), explains that you can pass a Backend
object to its quantum_instance
argument like this:
vqc = VQC(
sampler=sampler,
feature_map=feature_map,
ansatz=ansatz,
optimizer=optimizer,
callback=callback_graph,
quantum_instance=braket_local_simulator # <-- You can pass the Backend object here (e.g. braket_local_simulator, braket_sv1)
)
I believe the rest of the code in your example does not need to be changed.
peterkomar-aws is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.