Amplitude Embedding Issue when running on Qiskit Device

Hello,

I get the following error when I use amplitude embedding with the normalize parameter set to True on a Qiskit device. I observe this error even when I normalize manually. I do not observe this error when using the default simulator device. How do I solve this issue?

Traceback (most recent call last):
  File "ensembles_ibm_qx.py", line 117, in <module>
    p.append(qnode(learners[j], features=X_valid[i]))
  File "/opt/miniconda3/lib/python3.8/site-packages/pennylane/interfaces/autograd.py", line 69, in __call__
    return self.evaluate(args, kwargs)
  File "/opt/miniconda3/lib/python3.8/site-packages/autograd/tracer.py", line 48, in f_wrapped
    return f_raw(*args, **kwargs)
  File "/opt/miniconda3/lib/python3.8/site-packages/pennylane/qnodes/base.py", line 826, in evaluate
    ret = self.device.execute(self.circuit, return_native_type=temp)
  File "/opt/miniconda3/lib/python3.8/site-packages/pennylane/_qubit_device.py", line 169, in execute
    self.apply(circuit.operations, rotations=circuit.diagonalizing_gates, **kwargs)
  File "/opt/miniconda3/lib/python3.8/site-packages/pennylane_qiskit/qiskit_device.py", line 214, in apply
    applied_operations = self.apply_operations(operations)
  File "/opt/miniconda3/lib/python3.8/site-packages/pennylane_qiskit/qiskit_device.py", line 265, in apply_operations
    gate = mapped_operation(*par)
  File "/opt/miniconda3/lib/python3.8/site-packages/qiskit/extensions/quantum_initializer/initializer.py", line 57, in __init__
    raise QiskitError("Sum of amplitudes-squared does not equal one.")
qiskit.exceptions.QiskitError: 'Sum of amplitudes-squared does not equal one.'

The following is a section of the code:

dev1 = qml.device(name='qiskit.ibmq', backend='ibmq_rome',
				  ibmqx_token='xxx',
				  hub='ibm-q-xxx', group='xxx-uni', project='main', wires=num_wires)

def one_layer(W):
	for i in range(num_wires):
		qml.Rot(W[i][0], W[i][1], W[i][2], wires=i)
	qml.CNOT(wires=[0, 1])
	qml.CNOT(wires=[1, 2])
	qml.CNOT(wires=[2, 3])
	qml.CNOT(wires=[3, 4])
	qml.CNOT(wires=[4, 0])

def classifier(weights, features=None):
	AmplitudeEmbedding(features=features, wires=range(num_wires), normalize=True)
	for W in weights:
		one_layer(W)
	return qml.expval(qml.PauliZ(0))

Qiskit version info is as follows:

PennyLane-qiskit==0.12.0
qiskit==0.23.1
qiskit-aer==0.7.1
qiskit-aqua==0.8.1
qiskit-ibmq-provider==0.11.1
qiskit-ignis==0.5.1
qiskit-terra==0.16.1

Thank you!

Hi @tpatel,

Welcome to the community, and thank you for reaching out!

I’m looking into this right now. So far I haven’t been able to reproduce the error - I’ve tried these two types of devices so far:

# Locally simulated device
dev1 = qml.device(name='qiskit.aer', wires=num_wires)

# IBM Q backend
dev1 = qml.device(name='qiskit.ibmq', backend='ibmq_qasm_simulator', wires=num_wires)

I don’t have access to Rome, but I am currently in the queue for a different hardware backend, and I will update this once that is finished. (Update: the job completed without errors, so please let me some more details about the data.)

In the meantime, I was wondering if you could provide a bit more information about the kind of data you’re working with. I tried with a very simple example, where I am using both normalize = True and pad = 0.0 in the AmplitudeEmbedding.

import pennylane as qml
import numpy as np

num_wires = 5

# Some random data
features = np.array([2, 3, 4, 5])

dev1 = qml.device(name='qiskit.aer', wires=num_wires)

def one_layer(W):
    for i in range(num_wires):
        qml.Rot(W[i][0], W[i][1], W[i][2], wires=i)
        qml.CNOT(wires=[0, 1])
        qml.CNOT(wires=[1, 2])
        qml.CNOT(wires=[2, 3])
        qml.CNOT(wires=[3, 4])
        qml.CNOT(wires=[4, 0])

@qml.qnode(dev1)
def classifier(weights, features=None):
    qml.templates.AmplitudeEmbedding(features=features, wires=range(num_wires), normalize=True, pad=0.0)
    for W in weights:
        one_layer(W)
    return qml.expval(qml.PauliZ(0))

weights = np.random.uniform(size=(1, num_wires, 3))

classifier(weights, features=features)

Does this run successfully on either the simulator or hardware backend you’re working with?

Hi @glassnotes,

Thank you for your help! It was in fact a precision issue with the numpy array. The numpy dataset I am using was stored as float64. But loading it on one of my devices (Python 3.7.4) was converting it into a float32 format. So casting it into float64 solved the issue.

To be on the safe side, should I always ensure a 64-bit precision when working with pennylane/pennylane-qiskit?

Thank you!

Hi @tpatel,

Awesome, glad you solved it!

I looked through our logs and it seems that there are some differences in how PennyLane and Qiskit calculate the norm of a statevector, which is likely why upping the precision fixed things (you can read more about the issues here if you’re interested). So probably a good idea to use float64 if you’re working with very dense data vectors that will require normalization.

Cheers!

1 Like