I would like to use the PauliFeatureMap from qiskit in the context of kernel algorithms, while being powered by pennylane’s automatic differentiation. Does anyone know if we can translate the feature map from qiskit into pennylane’s framework? i looked into qml.from_qiskit(), but I had troubles because the feature map depends on inputs (the circuit is not fixed). On the other hand, maybe it would be easier to code the feature map directly in pennylane, but I liked the flexibility of the qiskit implementation.
Does anyone know if we can translate the feature map from qiskit into pennylane’s framework?
This should be possible however I had a go at using PauliFeatureMap with qml.from_qiskit() today and couldn’t get things to work. I’ve passed this onto the team who works on the PennyLane-Qiskit plugin and will update you if we get it working.
i looked into qml.from_qiskit(), but I had troubles because the feature map depends on inputs (the circuit is not fixed)
The qml.from_qiskit() function should in principle allow you to easily add-in differentiability, see the example below for a simple circuit:
from qiskit import QuantumCircuit
from qiskit.circuit import Parameter
import pennylane as qml
dev = qml.device("default.qubit", wires=2)
theta = Parameter("theta")
qc = QuantumCircuit(2)
qc.rx(theta, [0])
qc.cx(0, 1)
@qml.qnode(dev)
def loaded_circuit(x):
qml.from_qiskit(qc)({theta: x})
return qml.expval(qml.PauliZ(0))
However, this seemed to error when adapting for PauliFeatureMap.
On the other hand, maybe it would be easier to code the feature map directly in pennylane
Thanks for bringing up this topic. Even I’ve been looking for FeatureMaps and Variational Circuits from qiskit in Pennylane (PauliFeatureMap, RealAmplitudes, EfficientSU2, etc.).
It would be of great help if you could share the class that you rewrote in pennylane. Thanks!
Hi @syedfarhan, can you provide some more detail on what is the functionality that you’re looking for?
For the RealAmplitudes and EfficientSU2 cases in particular you can take a look at the PennyLane templates. They provide very good usage flexibility so you may be able to tailor one of the templates to do what you need.
Please let us know if you need guidance onto how to use the templates.
If you think it’s a feature that others might want to use, it would be really cool if you could contribute this to PennyLane. We can guide you on how to contribute (if you need it) or you can read our development guidelines.
If you don’t want to contribute this to PennyLane directly but want to share your code here that may also help other people.
Hey @kevinkawchak , could you please share the full error output and the package versions you’re using?
You can use this icon
right above where you write your reply to embed code in your post.
can you tell me how to use the RealAmplitudes from qiskit in pennylane, like the following :
ansatz = RealAmplitudes(num_qubits=num_qubits, entanglement='linear', reps=3)
i used the following code to be as VQC circuit but it is not working :
@qml.qnode(dev)
def circuit(params, x):
# Feature map (embedding classical data to quantum state)
qml.IQPEmbedding(x, wires=range(num_qubits))
# Dynamically assign parameters to Qiskit circuit
parameter_dict = dict(zip(qiskit_circuit.parameters, params))
bound_circuit = qiskit_circuit.assign_parameters(parameter_dict)
# Map Qiskit circuit to PennyLane explicitly without `wires`
qml.from_qiskit(bound_circuit)
# Measure expectation value of PauliZ on qubit 0
return qml.expval(qml.PauliZ(0))
If you want to use qml.from_qiskit() you need to use it on your full Qiskit circuit. So you’d need to add the ansatz to the circuit first and then use qml.from_qiskit() following the example there in the docs.