Skip to content

Commit

Permalink
Initial commit cesga_tnbs
Browse files Browse the repository at this point in the history
  • Loading branch information
gonfeco committed Jun 27, 2024
1 parent 7ed8383 commit 976e133
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 0 deletions.
133 changes: 133 additions & 0 deletions misc/inverse_circuit/gates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import numpy as np
import functools as ft

zero_ket = np.array([1, 0])
one_ket = np.array([0, 1])

def m_operator(inaccuracy):
"""
Matrix M for indetermination using CF
"""
theta = inaccuracy * np.pi / 2
return np.array([
[np.cos(theta), np.sin(theta)],
[np.sin(theta), -np.cos(theta)]
])

def pauli_operators(pauli_index):
"""
Return the correspondent Pauli matrix.
Parameters
----------
pauli_index : int
Number for selecting the Pauli matrix:
0 -> identity, 1-> X, 2-> Y, 3 -> Z
Returns
-------
pauli = np.array
2x2 Pauli Matrix
"""
if pauli_index == 0:
pauli = np.identity(2, dtype=np.complex128)
elif pauli_index == 1:
pauli = np.array([[0, 1], [1, 0]])
elif pauli_index == 2:
pauli = np.array([[0, -1j], [1j, 0]])
elif pauli_index == 3:
pauli = np.array([[1, 0], [0, -1]])
return pauli

def toffoli():
"""
Toffoli gate. In CF this the AND gate
"""
toffoli_ = np.identity(8)
toffoli_[6,6] = 0.0
toffoli_[7,7] = 0.0
toffoli_[6,7] = 1.0
toffoli_[7,6] = 1.0
return toffoli_

def cnot_gate(nqubits, control, target):
lista0 = []
lista1 = []
for i in range(nqubits):
if i == control:
lista0.append(np.outer(zero_ket, zero_ket))
lista1.append(np.outer(one_ket, one_ket))
elif i == target:
lista0.append(pauli_operators(0))
lista1.append(pauli_operators(1))
else:
lista1.append(pauli_operators(0))
lista0.append(pauli_operators(0))
return ft.reduce(np.kron, lista0) + ft.reduce(np.kron, lista1)

def not_gate():
"""
Not gate in CF
"""
return np.kron(pauli_operators(0), pauli_operators(1)) @ cnot_gate(2, 0, 1)

def or_gate():
toff_ = toffoli()
first_cnot = cnot_gate(3, 0, 2)
second_cnot = cnot_gate(3, 1, 2)

return second_cnot @ first_cnot @ toff_


def state(lista):
"""
Given a list it creates the corresponding state vector
"""
zero_ket = np.array([1, 0])
one_ket = np.array([0, 1])
ket = []
for i in lista:
if i == 0:
ket.append(zero_ket)
elif i == 1:
ket.append(one_ket)
else:
raise ValueError("Only can be 0 o0r 1")
return ft.reduce(np.kron, ket)

def implication(certainty):
"""
Matrix for implication operator
"""
before = ft.reduce(
np.kron,
[pauli_operators(0), m_operator(certainty), pauli_operators(0)]
)

return toffoli() @ before

if __name__ == "__main__":

print("Test NOT gate")
print(not_gate() @ state([0, 0]) == state([0, 1]))
print(not_gate() @ state([1, 0]) == state([1, 0]))
print("Test AND gate")
print(toffoli() @ state([0, 0, 0]) == state([0, 0, 0]))
print(toffoli() @ state([1, 0, 0]) == state([1, 0, 0]))
print(toffoli() @ state([0, 1, 0]) == state([0, 1, 0]))
print(toffoli() @ state([1, 1, 0]) == state([1, 1, 1]))
print("Test OR gate")
print(or_gate() @ state([0, 0, 0]) == state([0, 0, 0]))
print(or_gate() @ state([1, 0, 0]) == state([1, 0, 1]))
print(or_gate() @ state([0, 1, 0]) == state([0, 1, 1]))
print(or_gate() @ state([1, 1, 0]) == state([1, 1, 1]))
print("Implcation Gate")
print(implication(1.0) @ state([1, 0, 1]))





34 changes: 34 additions & 0 deletions misc/inverse_circuit/zalo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
Reverse inferential circuits
"""
import numpy as np
import functools as ft
from gates import *


def funcion(certainty, alpha):
"""
Contruyo Implicacion. Fijo precision del hecho antecedente
Quiero saber la precision de salida (dar output_precision =0)
"""
# El primer qubit es una combinacion lineal
#first_qubit = np.sqrt(alpha) * state([0]) + np.sqrt(1-alpha) * state([1])
first_qubit = m_operator(alpha) @ state([0])
input_vector = ft.reduce(np.kron,[first_qubit, state([0]), state([0])])
input_vector = implication(certainty) @ input_vector
list_finals = []
for state_ in [[0, 0, 1], [0, 1, 1], [1, 0, 1], [1, 1, 1]]:
list_finals.append(np.real(state(state_).T @ input_vector) **2)
return np.sum(list_finals)
x = 0.8
print(funcion(x, 0.8))

def loss(x, alpha, output_precision):
return abs(funcion(x[0], alpha) - output_precision)

# from scipy.optimize import minimize
#
# x0 = [0.5] #, 0.5]
# bnds = ((0, None))
# res = minimize(loss, [0.5], args = (0.7, 0.6))
# print(res)

0 comments on commit 976e133

Please sign in to comment.