Converting Numpy matrix in Qobj

2,162 views
Skip to first unread message

Simone Azeglio

unread,
Jun 13, 2017, 4:17:55 AM6/13/17
to QuTiP: Quantum Toolbox in Python
Hello! 
I'm trying to implement a Quantum PageRank algorithm with QuTip. The main problem is that I'm using NetworkX to extract the Google Matrix of my network and the return type is a Numpy matrix (ref: https://networkx.github.io/documentation/development/reference/generated/networkx.algorithms.link_analysis.pagerank_alg.google_matrix.html).

Well the problem is that I have to do a tensor product between G-matrix elements and a ket that I have defined with qutip.states.basis, but being the G-matrix a Numpy matrix I cannot do it. 

Thank you in advance for your help ! 

The code is :


...
G = nx.google_matrix(ER) #ER is my network , defined before 

#Projection operator Pi
piOperator = qt.states.projection(N*N, 1, 1)

#Psi0 state 
Psi0 = qt.states.zero_ket(N*N)

for i in xrange(0 , N-1 ):
    auxKet = qt.states.basis(N, i) #Auxiliary vector 
    Psi0 = qt.tensor(auxKet,np.sqrt(G[:,i]))

jkur...@gmail.com

unread,
Jul 14, 2017, 9:13:00 AM7/14/17
to QuTiP: Quantum Toolbox in Python
Good Morning,

I've also had to work with outside data that are arrays/matrices. Here is what I've found to work (for what it's worth).

Qobj to numpy array / matrix:
data = q.full()
array = numpy.array(data)
matrix = numpy.matrix(data)

Numpy to Qobj
q = Qobj(array)
q = Qobj(matrix)

Numpy operations:
Matrix multiplication in numpy is '*" for matricies and (for 2d) numpy.dot(). (e.g. A.dot(B) for arrays A * B for matrices)
Element wise squaring is numpy.square() (works regardless of the dims)

Tensor on numpy things (not Qobjs):
We've been using numpy.kron(a,b) to tensor a and b (if I remember correctly this works with arrays and matrices)

Don't know if this is helpful not. :)

V/r
JK

pietro Locatelli

unread,
Oct 14, 2021, 9:56:12 AM10/14/21
to QuTiP: Quantum Toolbox in Python

It was very useful.
Thank you very much
Reply all
Reply to author
Forward
0 new messages