Train qiskit circuit

I have circuit in qiskit that I want to optimize in Penny Lane. Using load function (https://pennylane.readthedocs.io/en/stable/code/api/pennylane.load.html) I get error AttributeError: ‘ArrayBox’ object has no attribute ‘items’

What is the problem?

qc = qiskit.QuantumCircuit(2)
theta = Parameter(“θ”)

qc.rz(theta, [0])
qc.cx(0, 1)

my_circuit = qml.load(qc, format=‘qiskit’)

theta_train = 0.4

opt = qml.GradientDescentOptimizer()

for i in range(1000):
theta_train = opt.step(my_circuit, theta_train)
if i % 100 == 0:
print(“Cost”, my_circuit(theta_train))

Hi @strawberry_lane! I was able to replicate your error and found that the main problem is that my_circuit is actually not yet a real circuit. It’s only yet a template. You can read more about templates here https://pennylane.readthedocs.io/en/stable/introduction/templates.html

In order for you to use it in the GradientDescentOptimizer you need to include your template in a QNode. For this you need to do the following steps
1 - Create a device.
2 - Use the @qml decorator to create the qnode.
3 - Define a function (the actual circuit) just below the qnode line. This function takes a parameter x.
4 - Define your my_circuit template inside the function. Make sure to assign the parameter theta to the input x of the function as a dictionary.

The rest stays the way you have it. The following would be the finished code.

#------------

qc = qiskit.QuantumCircuit(2)
theta = Parameter(“θ”)

qc.rz(theta, [0])
qc.cx(0, 1)

my_circuit = qml.load(qc, format=‘qiskit’)

dev = qml.device(‘default.qubit’, wires=2)

@qml.qnode(dev)
def circuit(x):
my_circuit(params={theta: x},wires=(1, 0))
return qml.expval(qml.PauliZ(0))

theta_train = 0.4

opt = qml.GradientDescentOptimizer()

for i in range(1000):
theta_train = opt.step(circuit, theta_train)
if i % 100 == 0:
print(“Cost”, circuit(theta_train))

#----------

Take into account that the cost is always 1 in this case because your qubit starts in 0 and you’re only rotating it around the Z axis which keeps it at zero. If you change the rotation to be around the X axis you will be able to see the optimization.

Please let me know if this helped!

thanks @CatalinaAlbornoz.

I have tried to copy your code and get strange error
File “”, line 2
theta = Parameter(“θ”)
^
SyntaxError: invalid character in identifier

I have put tabs for indented lines but still no luck.

Hi @strawberry_lane!
This often happens to me too. When you copy and paste code some characters can get pasted in the wrong format. In this case the quotation marks are tilted instead of straight, so they are not in the correct format for python to read. Thus, it understands them as an invalid character.

The solution is easy. You just need to erase the quotation marks and write them yourself. This way they will be in the correct format.

Make sure you change both the double and the single quotation marks in lines 2, 7, 9 and 23.

Please let me know if this works for you!

thanks @CatalinaAlbornoz.

I have managed to get it working by editing quotation marks. Your suggestion to use qc.rx gate is also useful.

Hi @strawberry_lane. You’re welcome! I’m glad I could help. Don’t hesitate to ask any other question you might have. And enjoy using PennyLane!