Casting of Complex to float when using PhaseShift in circuit for QML

Hello!

I am currently facing the issue that when I include PhasShift gates in my circuit im my QML project I get a ‘casting imaginary to real’ warning. This warning emerges when calling the step_and_cost method of ADAM.

from pennylane import numpy as np
import pennylane as qml
from pennylane.optimize import AdamOptimizer

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

# create random data with complex amplitudes and binary classification label
X_train = np.random.rand(100, 2) + 1j * np.random.rand(100, 2)
Y_train = np.random.randint(low = 0, high=2, size=(100,1))

# define circuit with phase shift gate
@qml.qnode(dev)
def shift_circuit(state, weight):
    qml.QubitStateVector(state, wires=range(1), normalize=True)

    qml.PhaseShift(weight[0], 0)
    
    return qml.expval(qml.PauliZ(0))

# cost function with MSE of batch prediction
def cost(weights, X, Y):
    predictions = np.array([shift_circuit(x, weights) for x in X])
    return np.sum((Y - predictions)**2)

opt = AdamOptimizer()

q_weights = np.ones(1)

# optimisation run with 
for it in range(3):
    batch_index = np.random.randint(0, len(X_train), (5,))
    X_batch = X_train[batch_index]
    Y_batch = Y_train[batch_index]
               
    q_weights, loss = opt.step_and_cost(cost, q_weights, X=X_batch, Y=Y_batch)

The warning message is the following and I get it for every iteration

/opt/miniconda3/lib/python3.12/site-packages/autograd/numpy/numpy_wrapper.py:156: ComplexWarning: Casting complex values to real discards the imaginary part
  return A.astype(dtype, order, casting, subok, copy)

Here is the qml.about() information:

Name: PennyLane
Version: 0.38.0
Summary: PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.
Home-page: GitHub - PennyLaneAI/pennylane: PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.
Author:
Author-email:
License: Apache License 2.0
Location: /opt/miniconda3/lib/python3.12/site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, packaging, pennylane-lightning, requests, rustworkx, scipy, toml, typing-extensions
Required-by: PennyLane_Lightning

Platform info: macOS-15.0.1-arm64-arm-64bit
Python version: 3.12.1
Numpy version: 1.26.4
Scipy version: 1.14.1
Installed devices:

  • lightning.qubit (PennyLane_Lightning-0.38.0)
  • default.clifford (PennyLane-0.38.0)
  • default.gaussian (PennyLane-0.38.0)
  • default.mixed (PennyLane-0.38.0)
  • default.qubit (PennyLane-0.38.0)
  • default.qubit.autograd (PennyLane-0.38.0)
  • default.qubit.jax (PennyLane-0.38.0)
  • default.qubit.legacy (PennyLane-0.38.0)
  • default.qubit.tf (PennyLane-0.38.0)
  • default.qubit.torch (PennyLane-0.38.0)
  • default.qutrit (PennyLane-0.38.0)
  • default.qutrit.mixed (PennyLane-0.38.0)
  • default.tensor (PennyLane-0.38.0)
  • null.qubit (PennyLane-0.38.0)

Thanks in advance!

Hi @FBec , welcome to the Forum!

Thank you for reporting this. We have indeed seen these warnings popping up sometimes. I’ll share it with the team so they’re aware of this case.

The warnings shouldn’t impact the correctness of the results, but they can indeed be annoying and make it harder to view outputs.

You can hide these warnings with the code below.

I’ll tag you in another message on this thread if the team determines that there’s a bug causing this. Let me know if this solves your issue or if you’re seeing any other weird behaviour.

import warnings
warnings.filterwarnings(action="ignore", category=np.ComplexWarning)

I hope this helps!

Hi @CatalinaAlbornoz,

thank you very much for the quick answer.
I mainly wanted to make sure that the correctness is still given, so this works for me!

1 Like