Hello!
I am trying to use quantum circuit to do some classification works (VQC).
My circuit work pretty well with local simulater cureently. And I think the next step is to test if it is robust to noisy environment.
I searched some pennylane’s tutorial, and changed my circuit into noisy version.
Since I would use IBM quantum device to do final teting in the future, I think it is reasonable to plugin their noise during training.
However I found that if I change my device from “default.qubit” into ‘qiskit.aer’, the traning time increases significantly.
Here is a reproducable example code:
import numpy as np
import pennylane as qml
from qiskit_ibm_provider import IBMProvider
from qiskit_aer.noise import NoiseModel
from jax.config import config
import jax
import jax.numpy as jnp
config.update("jax_enable_x64", True)
jax.config.update('jax_platform_name', 'cpu')
n=100
provider = IBMProvider()
backend = 'ibmq_belem'
noise_model = NoiseModel.from_backend(provider.get_backend(backend)) #backend = provider.get_backend(backend)
dev = qml.device('qiskit.aer', wires=2, noise_model=noise_model)
#dev = qml.device("default.qubit", wires=2)
@qml.qnode(dev, interface="jax")
def circuit(p,x,y):
qml.RY(p[0]*x, wires=[0])
qml.RX(p[1]*y, wires=[0])
qml.CNOT(wires=[0, 1])
return qml.expval(qml.PauliZ(wires=1)),qml.expval(qml.PauliY(wires=1))
params = jnp.array([0.1,0.2])
datax = jnp.array(np.random.randn(2, n))
def loss(p,x):
rst = circuit(p, x[0], x[1])
cost=jnp.sum(sum(rst)-1)**2
return cost
And use %timeit
to see the performance difference:
%timeit cost, grad = jax.value_and_grad(loss)(params,datax)
One may see that the ‘default.qubit’ device is over hundred times faster than 'qiskit.aer’ device. I wonder what cause this difference, they should be having the similar scale of matrix multiplying.
I have tried to use jax.jit function to speedup training. But this will cause one problem:
A noisy circuit means there are some noise fluctuating when running, hence every time you run will get different outcome.
However if I use jit function, either jit the circuit or the grad function, the noise seems to be ‘fixed’. No matter how many times i run, I always get the same results.
Is there any way to speed up the training?
Any suggestion would be appreciated, thanks!
Here is the package imformation of qml.about()
:
Name: PennyLane
Version: 0.30.0
Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc.
Home-page: https://github.com/XanaduAI/pennylane
Author:
Author-email:
License: Apache License 2.0
Location: /home/ubuntu2022/anaconda3/envs/research/lib/python3.10/site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, rustworkx, scipy, semantic-version, toml
Required-by: PennyLane-Lightning, PennyLane-qiskit
Platform info: Linux-5.15.90.1-microsoft-standard-WSL2-x86_64-with-glibc2.35
Python version: 3.10.9
Numpy version: 1.23.5
Scipy version: 1.10.1
Installed devices:
- qiskit.aer (PennyLane-qiskit-0.30.1)
- qiskit.basicaer (PennyLane-qiskit-0.30.1)
- qiskit.ibmq (PennyLane-qiskit-0.30.1)
- qiskit.ibmq.circuit_runner (PennyLane-qiskit-0.30.1)
- qiskit.ibmq.sampler (PennyLane-qiskit-0.30.1)
- lightning.qubit (PennyLane-Lightning-0.30.0)
- default.gaussian (PennyLane-0.30.0)
- default.mixed (PennyLane-0.30.0)
- default.qubit (PennyLane-0.30.0)
- default.qubit.autograd (PennyLane-0.30.0)
- default.qubit.jax (PennyLane-0.30.0)
- default.qubit.tf (PennyLane-0.30.0)
- default.qubit.torch (PennyLane-0.30.0)
- default.qutrit (PennyLane-0.30.0)
- null.qubit (PennyLane-0.30.0)