Hello all,
I am trying to compile an arbitrary unitary to a quantum circuit using qml.HilbertSchmidt. The training is successful (converges to 10^(-5)), but when i transform the circuit to a matrix, the trace distance between the target unitary is large (2). Does anyone know what is going on?
And i know it is possible since they do it in a paper https://journals.aps.org/prx/abstract/10.1103/PhysRevX.11.041036
For instance
U = [[1,0,0,0],[0,sqrt(2)/2,sqrt(2)/2,0],[0,-sqrt(2)/2,sqrt(2)/2,0],[0,0,0,1]]
N= 2
depth = 5
u_wires = np.arange(N)
v_wires = np.arange(N,2*N)
with qml.tape.QuantumTape(do_queue=False) as u_tape:
qml.QubitUnitary(U, wires=u_wires)
def v_function(weights,wires):
D = np.shape(weights)[-1]
for d in range(D):
#rotation
for i in range(N):
qml.RY(weights[i,it,d],wires=wires[i])
for i in range(N-1):
qml.CNOT(wires=[wires[i],wires[(i+1)%N]])
dev = qml.device("lightning.qubit", wires=2*N)
@qml.qnode(dev)
def hilbert_test(v_params, v_function, v_wires, u_tape):
qml.HilbertSchmidt(v_params, v_function=v_function, v_wires=v_wires, u_tape=u_tape)
return qml.probs(u_tape.wires + v_wires)
def cost_hst(parameters, v_function, v_wires, u_tape):
return (1 - hilbert_test(v_params=parameters, v_function=lambda p:v_function(p,v_wires), v_wires=v_wires, u_tape=u_tape)[0])
loss_function = lambda v_params: cost_hst(v_params, v_function, v_wires, u_tape)
opt = qml.GradientDescentOptimizer(stepsize=0.04)
parameters = qml.numpy.array(np.random.normal(0,1,size=(N,1,depth)), requires_grad=True)
cost = []
for _ in range(200):
parameters = opt.step(loss_function, parameters)
cost.append(loss_function(parameters))
def f(params,wires):
v_function(params,wires)
but if i then print the corresponding matrix with the trained parameters, using the technique explained here Printing the matrix of a circuit during optimization, i obtain
[[-0.38304712+0.28415248j 0.44274339+0.20951309j 0.02119074+0.46715616j
-0.51994317+0.20878219j]
[-0.20400715-0.52963304j 0.54954541-0.55876896j -0.0565359 +0.10391468j
0.1190677 -0.18834896j]
[-0.18387383+0.2727543j -0.03200142+0.07518786j -0.27096935+0.5267104j
0.72912911-0.0513877j ]
[-0.42870162+0.39801083j 0.35100433+0.12640534j 0.0427307 -0.64393117j
0.20939861-0.24145166j]]
which is far away from what i am looking for (U).
Thanks
Cheers
Oriel