Scaling variables inside QNode quantum operations


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)


Hi @mxn.wls!

Scaling a variable by a scalar is supported in PennyLane:

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


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

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


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


TypeError Traceback (most recent call last)
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/ in wrapper(*args, **kwargs)
151 def wrapper(*args, **kwargs):
152 “”“Wrapper function”""
–> 153 return qnode(*args, **kwargs)
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/ 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
459 @ae.primitive

~/anaconda3/envs/vqa/lib/python3.6/site-packages/autograd-1.2-py3.6.egg/autograd/ in f_wrapped(*args, **kwargs)
46 return new_box(ans, trace, node)
47 else:
—> 48 return f_raw(*args, **kwargs)
49 = 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/ in evaluate(self, args, **kwargs)
512 ret = self.device.execute(self.queue, self.ev)
–> 513 return self.output_type(ret)
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
cd pennylane-forest
pip install -e .