Hi @CatalinaAlbornoz ,
Thank you very much! I was really lost.
It is way clearer now.
I have a couple of questions. My Pennylane version is 0.42.0-dev52.
Question 1: I’m using qml.GQSP for an arbitrary polynomial that I took from the documentation. However, the output that I’m computing using the classical SVD using scipy doesn’t tally with the output from GQSP? I’ve checked whether the matrix is Hermitian and it is Hermitian. My code is as below.
import numpy as np
import pennylane as qml
import scipy
# Qiskit is just used for visualization
from qiskit.visualization import array_to_latex
A = np.array([[-0.1, 0, 0, 0.1], [0, 0.2, 0, 0], [0, 0, -0.2, -0.2], [0.1, 0, -0.2, -0.1]])
print("Is A Hermitian:", scipy.linalg.ishermitian(A))
# P(x) = 0.1 + 0.2j x + 0.3 x^2
poly = [0.1, 0.2j, 0.3]
angles = qml.poly_to_angles(poly, "GQSP")
dev = qml.device("default.qubit")
@qml.prod # transforms the qfunc into an Operator
def unitary(wires):
qml.BlockEncode(A, wires=wires)
@qml.qnode(dev)
def circuit():
qml.GQSP(unitary(wires=[0,1,2]), angles, control = 3)
return qml.state()
qml.draw_mpl(circuit)()
W, s, Vh = scipy.linalg.svd(A)
expected_output = np.round(W @ scipy.linalg.diagsvd(np.poly1d(poly[::-1])(s), *A.shape) @ Vh,3)
print("Expected output real:\n")
display(array_to_latex(expected_output.real, max_size=10000))
print("Expected output imaginary:\n")
display(array_to_latex(expected_output.imag, max_size=10000))
matrix = qml.matrix(circuit, wire_order=[0,1,2,3])()
print("GQSP output real\n")
display(array_to_latex(np.round(matrix[:4, :4].real, 3), max_size=10000))
print("GQSP output imaginary\n")
display(array_to_latex(np.round(matrix[:4, :4].imag, 3), max_size=10000))
Output:
Question 2: You mentioned that if I use Hermitian (square matrix), then I can use GQSP.
For an arbitrary matrix A (not necessarily Hermitian), can I do Hermitian dilation as following, block encode H using BlockEncode or Fabel and then use it with GQSP?
Question 3: Why doesn’t qml.poly_to_angles allow a degree 0 polynomial such as P(x) = 1x^0? If I run the following code:
import pennylane as qml
poly = [1]
angles = qml.poly_to_angles(poly, "GQSP")
there will be an error AssertionError: The polynomial must have at least degree 1. I’m thinking of a use case to do a polynomial transformation like WPoly(S)V = WIV = WV where I is the identity matrix and Poly(S)=1.
Question 4: As far as I know, pyqsp doesn’t have the ability to generate angles for GQSP which expects
- angles (tensor[ float ]) – array of angles defining the polynomial transformation. The shape of the array must be (3, d+1), where d is the degree of the polynomial.
Also, pyqsp doesn’t accept function with complex coefficients such as func = lambda x: 0.1 + 0.2j*x + 0.3*x**2
On the other hand, Pennylane doesn’t have a feature to generate polynomial for an arbitrary function unless I generate phase angles from optimization as done in QSVT in Practice or if I already know the coefficients of the function.
Hence, just restricting to real coefficients, I am generating the angles using the following code:
import pennylane as qml
from pyqsp.poly import PolyTaylorSeries
# Some arbitrary function
# pyqsp doesn't accept complex coefficients
# func = lambda x: 0.1 + 0.2j*x + 0.3*x**2
func = lambda x: 0.1 + 0.2*x + 0.3*x**2
poly, scale = PolyTaylorSeries().taylor_series(
func=func,
degree=11, # EVEN degree like 10 doesn't work
max_scale=1.0,
chebyshev_basis=True,
ensure_bounded=True,
return_scale=True,
cheb_samples=200)
# Pennylane expects polynomial in standard basis - https://discuss.pennylane.ai/t/error-when-using-qml-qsvt/8612/5?u=jag
target_poly = np.polynomial.chebyshev.cheb2poly(poly.coef.tolist())
angles = qml.poly_to_angles(target_poly, "GQSP")
Is this an acceptable way to generate the angles to be used with GQSP?
Also, poly_to_angles throw an error ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (2,) + inhomogeneous part. when I use an even number, e.g. 10, as the degree argument value in the code above.
Thank you again for your kind help.




