# Problem in sf.apps.similarity.prob_orbit_exact() function

Hey!

So I am trying to code the probability function in equation (3) of this paper and to recheck my calculation, I pass a sample of all 0s in my probability code and recheck it with sf.apps.similarity.prob_orbit_exact by passing in an all 0 sample as well. As we know that an all zero sample orbit can only have one sample (all 0), I am getting the probabilities different from these 2 methods. Could you please look into this? Here is the code:-

def probzu(A,S):
p=1
k= np.repeat(A, S, axis=0)
A1= np.repeat(k, S, axis=1)
z= np.abs(haf(A1)) ** 2
_, s, _ = np.linalg.svd(A, full_matrices=True)
c = 1 / ( np.max(s) + 1e-8 )
Ab = c * np.block([
[A, np.zeros(A.shape)],
[np.zeros(A.shape), A]
])

``````X = np.block([
[np.zeros(A.shape), np.identity(A.shape)],
[np.identity(A.shape), np.zeros(A.shape)]
])

I = np.identity(Ab.shape)
Q = np.linalg.inv(I - X @ Ab)
B = math.sqrt(np.linalg.det(Q))
for i in range(len(S)):
p= p*math.factorial(S[i])
prob= z/(p*B)
return prob
``````

A= np.array([[0,0,1,0,1],
[0,0,0,1,1],
[1,0,0,1,1],
[0,1,1,0,0],
[1,1,1,0,0]])

print(probzu(A,[0,0,0,0,0]))
print(sf.apps.similarity.prob_orbit_exact(nx.from_numpy_array(A),[0,0,0,0,0]))

Thank you for your question. I’m not sure of your implementation. Could you please comment your code so that I can understand it better?

I’m also not sure that all of the assumptions hold here. Note that for the equation that you mention it’s assumed that the displacement ‘d’ is zero. I’m not sure whether or not this is the case in your example.

Hey!

Apologies for that, Here you go!

def probzu(A,S): #A is the adjacency matrix of the graph and S is the sample
p=1
k= np.repeat(A, S, axis=0)
A1= np.repeat(k, S, axis=1) #A1 is An of the paper where the rows and columns are repeated as per the sample
z= np.abs(haf(A1)) ** 2 #The hafnian calculation of An
_, s, _ = np.linalg.svd(A, full_matrices=True)
c = 1 / ( np.max(s) + 1e-8 ) #Calculation of the maximum singular value of A
Ab = c * np.block([ #Calculating Q so building \Tilde{A} for that
[A, np.zeros(A.shape)],
[np.zeros(A.shape), A]
])

``````X = np.block([                  #The X matrix
[np.zeros(A.shape), np.identity(A.shape)],
[np.identity(A.shape), np.zeros(A.shape)]
])

I = np.identity(Ab.shape)
Q = np.linalg.inv(I - X @ Ab)          #Calculating Q as per paper
B = math.sqrt(np.linalg.det(Q))        #Square root of determinent of Q
for i in range(len(S)):
p= p*math.factorial(S[i])          #factorial n! of the sample
prob= z/(p*B)                          #probability calculation with hafian divided by sqrt(detQ)*factorial
return prob
``````

A= np.array([[0,0,1,0,1], #example
[0,0,0,1,1],
[1,0,0,1,1],
[0,1,1,0,0],
[1,1,1,0,0]])

print(probzu(A,[0,0,0,0,0])) #passing the example with a sample all 0

print(sf.apps.similarity.prob_orbit_exact(nx.from_numpy_array(A),[0,0,0,0,0])) #all-zero orbit probability

probzu is my function that calculates probability as per eq.3 of paper. Yes my displacement is 0 too!

Apologies for the inconvenience.

Hello! Sorry to disturb but are there any updates on this?

do take your time, I was just curious😊

Hey @MUSHKAN_SUREKA! Apologies for the delayed response. I’m honestly having a hard time seeing how your implementation is not matching the output of `prob_orbit_exact`… It might be a good idea to check out some nice functions in another software package Xanadu has called The Walrus: The Walrus — The Walrus 0.20.0 documentation

Specifically, check out reduction as a means to calculate A_n instead of your built-in method. There is also a function for calculating the hafnian.

Let me know if these help!

I tried the hafnian and it still gives the same different values. One of the Xanadu members helped me before to code A_n and that is the code I am using Hey @MUSHKAN_SUREKA! I’m working on figuring this out, it just might take a bit more time. Apologies for the delay! I will get back to you as soon as I can figure out what the issue is.

Hey @MUSHKAN_SUREKA, sorry for the delay here. I reached out to the original authors of the paper and the `prob_orbit_exact` function that you’re comparing your code to. It will probably be a little bit until we get a response from them as they’re very busy .

That being said, this is on my radar and I will make sure to provide updates as soon as I get them .

I apologise deeply to be disturbing you guys like this ;-; . Please do take your time No worries at all! This is what we’re here for !