Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support loading Parameter Expressions #129

Closed
albi3ro opened this issue Feb 2, 2021 · 2 comments
Closed

Support loading Parameter Expressions #129

albi3ro opened this issue Feb 2, 2021 · 2 comments
Labels
enhancement New feature or request

Comments

@albi3ro
Copy link
Contributor

albi3ro commented Feb 2, 2021

Currently, the load function to convert a Qiskit circuit cannot convert Qiskit ParameterExpression's with more than a simple, unaltered Parameter. See error raised at converter.py#L205

PennyLane now, as of v0.14.0, supports classical processing of arguments within a circuit. With this change, PennyLane-Qiskit should update their support of ParameterExpression's.

We should be able to do the following:

from qiskit import QuantumCircuit
from qiskit.circuit import Parameter
from pennylane_qiskit import load

a = Parameter("a")
b = Parameter("b")

qc = QuantumCircuit(1,1)
qc.rz(a*b, [0])

circ = load(qc)(params={a: 0.1, b: 0.1})

Potential Solution

ParameterExpression's are based on SymPy, and thus we can use SymPy functions with them.

from sympy import lambdify

expr = a*b
f = lambdify([a,b], expr, 'numpy')
f(0.1, 0.1)

The resulting function is differentiable in pennylane:

import pennylane as qml

qml.grad(f)(1.0, 1.0)
@josh146 josh146 added the enhancement New feature or request label Feb 2, 2021
@antalszava
Copy link
Contributor

@albi3ro has this been resolved?

@lillian542
Copy link
Contributor

This is supported now - for example, we can do:

from qiskit import QuantumCircuit
from qiskit.circuit import Parameter
from pennylane_qiskit import load

dev = qml.device("default.qubit")

a = Parameter("a")
b = Parameter("b")

qc = QuantumCircuit(1,1)
qc.rx(a*b, [0])

@qml.qnode(dev)
def circuit(x, y):
    load(qc)(params={a: x, b: y})
    return qml.expval(qml.PauliZ(0))

and run

>>> circuit(0, np.pi)
tensor(1., requires_grad=True)

>>> circuit(1, np.pi)
tensor(-1., requires_grad=True)

>>> circuit(0.5, np.pi)
tensor(2.22044605e-16, requires_grad=True)

The circuit is differentiable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants