Scaling variables inside QNode quantum operations

Hey,

First off, thanks for the great work developing & providing Pennylane.

I think I have understood enough to know that this can’t be done in Pennylane, though v. happy to be told I am wrong.

Can I scale the Variable in optimisation by a sign or scalefactor?

E.g. for maxsat QAOA.

I want to implement a quantum algorithm (qaoa with p=1) for a maxsat problem. This has 2 parameters. The operation dependent on the one of the parameters (gamma) involves changing the sign (and scalefactor) for different qubits.

For example if C = z1 - z2
The sequence of gates will be RZ(+gamma, z1), RZ(-gamma, z2)

Thanks!

Hi @mxn.wls!

Scaling a variable by a scalar is supported in PennyLane:

@qml.qnode(dev)
def circuit(gamma):
    qml.RZ(gamma, wires=0)
    qml.RZ(gamma*-1, wires=1)
    return qml.expval.PauliX(0)

Note, however, that arithmetic between two free parameters is currently not supported.

1 Like

Thanks!

Is the following behaviour because the qiskit backend only supports measurements in the computational basis?

In:
N = 2
dev = qml.device(‘qiskit.basicaer’, wires=N)
@qml.qnode(dev)
def circuit(z):
qml.RZ(z, wires=[0])
qml.RZ(z*-1, wires=[0])
return qml.expval.PauliZ(wires=1)
circuit(0.2)

Out:
1.0

In:
import pennylane as qml
N = 2
dev = qml.device(‘qiskit.basicaer’, wires=N)
@qml.qnode(dev)
def circuit(z):
qml.RZ(z, wires=[0])
qml.RZ(z*-1, wires=[0])
return qml.expval.PauliX(wires=1)
circuit(0.2)

Out:

TypeError Traceback (most recent call last)
in
7 qml.RZ(z*-1, wires=[0])
8 return qml.expval.PauliX(wires=1)
----> 9 circuit(0.2)

~/anaconda3/envs/vqa/lib/python3.6/site-packages/PennyLane-0.3.1-py3.6.egg/pennylane/decorator.py in wrapper(*args, **kwargs)
151 def wrapper(*args, **kwargs):
152 “”“Wrapper function”""
–> 153 return qnode(*args, **kwargs)
154
155 # bind the jacobian method to the wrapped function

~/anaconda3/envs/vqa/lib/python3.6/site-packages/PennyLane-0.3.1-py3.6.egg/pennylane/qnode.py in call(self, *args, **kwargs)
455 # pylint: disable=no-member
456 args = autograd.builtins.tuple(args) # prevents autograd boxed arguments from going through to evaluate
–> 457 return self.evaluate(args, **kwargs) # args as one tuple
458
459 @ae.primitive

~/anaconda3/envs/vqa/lib/python3.6/site-packages/autograd-1.2-py3.6.egg/autograd/tracer.py in f_wrapped(*args, **kwargs)
46 return new_box(ans, trace, node)
47 else:
—> 48 return f_raw(*args, **kwargs)
49 f_wrapped.fun = f_raw
50 f_wrapped._is_autograd_primitive = True

~/anaconda3/envs/vqa/lib/python3.6/site-packages/PennyLane-0.3.1-py3.6.egg/pennylane/qnode.py in evaluate(self, args, **kwargs)
511
512 ret = self.device.execute(self.queue, self.ev)
–> 513 return self.output_type(ret)
514
515 def evaluate_obs(self, obs, args, **kwargs):

TypeError: float() argument must be a string or a number, not ‘NoneType’

Hi @mxn.wls, unfortunately this is a known issue with the Qiskit plugin, due to the recent release in the last couple of days of Qiskit versions 0.9 and 0.10.

We are in the process of updating the plugin to ensure it works with the latest version of Qiskit - in the meantime, you could check out the PennyLane-Forest plugin, which also allows qubit simulations with noise.

Note that the Forest plugin currently must be installed by cloning from the GitHub repository directly:

git clone https://github.com/rigetti/pennylane-forest.git
cd pennylane-forest
pip install -e .
2 Likes