import sympy as sp
f0, f0L, f1, f1L = sp.symbols('f0 f0L f1, f1L');
Or = sp.Matrix([[f0L,  f0,   0,   0],
                [ f0, f0L,   0,   0],
                [  0,   0, f1L,  f1],
                [  0,   0,  f1, f1L]])
Or

import sympy as sp
from sympy import kronecker_product as kron
H = sp.Matrix([[1,1],[1,-1]])
I = sp.Matrix([[1,0],[0,1]])

x=sp.Matrix([[1,0]])
y=sp.Matrix([[0,1]])

#ATENÇÃO: |yx> -> <xy| ou |q2q1q0> -> <q0q1q2| (equivalência ket-bra)
#Logo se utilizamos "bras" no lugar de "kets", o Qiskit deve ser lido
#de cima para baixo e da esquerda para a direita.

deustch = kron(x,y)*(kron(H,H))*(Or)*(kron(H,I))
display(deustch.T)

deustch.subs({f0:1, f0L:0, f1:1, f1L:0})

deustch.subs({f0:0, f0L:1, f1:0, f1L:1})

deustch.subs({f0:0, f0L:1, f1:1, f1L:0})

deustch.subs({f0:1, f0L:0, f1:0, f1L:1})

import numpy as np
x = np.array([[1,0]])
y = np.array([[0,1]])

xy = np.kron(x,y); xy
H2 = np.array([[1, 1],
              [1,-1]]); H2
I2 = np.array([[1,0],[0,1]]); I2
H2H2 = np.kron(H2,H2); H2H2

print(xy.dot(H2H2))

Or1 = np.array([[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]); Or1
res1 = xy.dot(H2H2).dot(Or1).dot(H2I2); print(res1)

Or2 = np.array([[0,1,0,0],[1,0,0,0],[0,0,0,1],[0,0,1,0]]); Or2
res2 = xy.dot(H2H2).dot(Or2).dot(H2I2); print(res2)

Or3 = np.array([[1,0,0,0],[0,1,0,0],[0,0,0,1],[0,0,1,0]]); Or3
res3 = xy.dot(H2H2).dot(Or3).dot(H2I2); print(res3)

Or4 = np.array([[0,1,0,0],[1,0,0,0],[0,0,1,0],[0,0,0,1]]); Or4
res4 = xy.dot(H2H2).dot(Or4).dot(H2I2); print(res4)

# Setup básico
!pip install qiskit -q
!pip install qiskit[visualization] -q
import qiskit as qk

!pip install qiskit-aer -q
import qiskit_aer as qk_aer

import numpy as np
np.set_printoptions(precision=3, suppress=True)

from matplotlib import pyplot as plt
%matplotlib inline

import pandas as pd
import sklearn as sk

# IMPORTANT NOTE: The readings in Qiskit should be done from bottom up.
#                 The most significant bit (the leftmost in a table or
#                 matrix is the one at the BOTTOM. To keep compatibility
#                 with the sympy and numpy matrices, x will be placed
#                 BELOW and y on TOP.
import numpy as np
Or1 = np.array([[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]); display(Or1)

import qiskit as qk
from qiskit.quantum_info.operators import Operator

qrx = qk.QuantumRegister(1,'x')
cr = qk.ClassicalRegister(1,"c")
qry = qk.QuantumRegister(1,'y')
qc = qk.QuantumCircuit(qry,qrx,cr)

qc.barrier()
qc.h(qrx)
qc.x(qry)
qc.h(qry)

qc.barrier()
oracle = Operator(Or1)
qc.append(oracle,[qry,qrx])

qc.barrier()
qc.h(qrx)
qc.measure(qrx,cr)

display(qc.draw('mpl'))

simulator = qk_aer.Aer.get_backend("aer_simulator")
results = simulator.run(qk.transpile(qc,simulator),shots=10000).result().get_counts()
qk.visualization.plot_histogram(results)

# IMPORTANT NOTE: The readings in Qiskit should be done from bottom up.
#                 The most significant bit (the leftmost in a table or
#                 matrix is the one at the BOTTOM. To keep compatibility
#                 with the sympy and numpy matrices, x will be placed
#                 BELOW and y on TOP.

import numpy as np
Or2 = np.array([[0,1,0,0],[1,0,0,0],[0,0,0,1],[0,0,1,0]]); display(Or2)

import qiskit as qk
from qiskit.quantum_info.operators import Operator

qrx = qk.QuantumRegister(1,'x')
cr = qk.ClassicalRegister(1,"c")
qry = qk.QuantumRegister(1,'y')
qc = qk.QuantumCircuit(qry,qrx,cr)

qc.barrier()
qc.h(qrx)
qc.x(qry)
qc.h(qry)

qc.barrier()
oracle = Operator(Or2)
qc.append(oracle,[qry,qrx])

qc.barrier()
qc.h(qrx)
qc.measure(qrx,cr)

display(qc.draw('mpl'))

simulator = qk_aer.Aer.get_backend("aer_simulator")
results = simulator.run(qk.transpile(qc,simulator),shots=10000).result().get_counts()
qk.visualization.plot_histogram(results)

# IMPORTANT NOTE: The readings in Qiskit should be done from bottom up.
#                 The most significant bit (the leftmost in a table or
#                 matrix is the one at the BOTTOM. To keep compatibility
#                 with the sympy and numpy matrices, x will be placed
#                 BELOW and y on TOP. When the function in balanced this
#                 is essential to get the correct measurement (x=<1|)

import numpy as np
Or3 = np.array([[1,0,0,0],[0,1,0,0],[0,0,0,1],[0,0,1,0]]); display(Or3)

import qiskit as qk
from qiskit.quantum_info.operators import Operator

qrx = qk.QuantumRegister(1,'x')
cr = qk.ClassicalRegister(1,"c")
qry = qk.QuantumRegister(1,'y')
qc = qk.QuantumCircuit(qry,qrx,cr)

qc.barrier()
qc.h(qrx)
qc.x(qry)
qc.h(qry)

qc.barrier()
oracle = Operator(Or3)
qc.append(oracle,[qry,qrx])

qc.barrier()
qc.h(qrx)
qc.measure(qrx,cr)

display(qc.draw('mpl'))

simulator = qk_aer.Aer.get_backend("aer_simulator")
results = simulator.run(qk.transpile(qc,simulator),shots=10000).result().get_counts()
qk.visualization.plot_histogram(results)

# IMPORTANT NOTE: The readings in Qiskit should be done from bottom up.
#                 The most significant bit (the leftmost in a table or
#                 matrix is the one at the BOTTOM. To keep compatibility
#                 with the sympy and numpy matrices, x will be placed
#                 BELOW and y on TOP. When the function in balanced this
#                 is essential to get the correct measurement (x=<1|)

import numpy as np
Or4 = np.array([[0,1,0,0],[1,0,0,0],[0,0,1,0],[0,0,0,1]]); display(Or4)

import qiskit as qk
from qiskit.quantum_info.operators import Operator

qrx = qk.QuantumRegister(1,'x')
cr = qk.ClassicalRegister(1,"c")
qry = qk.QuantumRegister(1,'y')
qc = qk.QuantumCircuit(qry,qrx,cr)

qc.barrier()
qc.h(qrx)
qc.x(qry)
qc.h(qry)

qc.barrier()
oracle = Operator(Or4)
qc.append(oracle,[qry,qrx])

qc.barrier()
qc.h(qrx)
qc.measure(qrx,cr)

display(qc.draw('mpl'))

simulator = qk_aer.Aer.get_backend("aer_simulator")
results = simulator.run(qk.transpile(qc,simulator),shots=10000).result().get_counts()
qk.visualization.plot_histogram(results)

import sympy as sp
f00, f00L, f01, f01L, f10, f10L, f11, f11L = \
     sp.symbols('f00 f00L f01, f01L f10 f10L f11, f11L');
Or = sp.Matrix([[f00L, f00 ,    0,    0,    0,    0,    0,    0],
                [f00 , f00L,    0,    0,    0,    0,    0,    0],
                [   0,    0, f01L, f01 ,    0,    0,    0,    0],
                [   0,    0, f01 , f01L,    0,    0,    0,    0],
                [   0,    0,    0,    0, f10L, f10 ,    0,    0],
                [   0,    0,    0,    0, f10 , f10L,    0,    0],
                [   0,    0,    0,    0,    0,    0, f11L, f11 ],
                [   0,    0,    0,    0,    0,    0,  f11, f11L]])
Or

import sympy as sp
from sympy import kronecker_product as kron
H = sp.Matrix([[1,1],[1,-1]])
I = sp.Matrix([[1,0],[0,1]])

x0=sp.Matrix([[1,0]])
x1=sp.Matrix([[1,0]])
y=sp.Matrix([[0,1]])

#ATENÇÃO: |yx> -> <xy| ou |q2q1q0> -> <q0q1q2| (equivalência ket-bra)
#Logo se utilizamos "bras" no lugar de "kets", o Qiskit deve ser lido
#de cima para baixo e da esquerda para a direita.

josza = kron(x0,kron(x1,y))*(kron(H,kron(H,H)))*(Or)*(kron(H,kron(H,I)))
display(josza.T)

josza.subs({f00:0, f00L:1,
            f01:0, f01L:1,
            f10:1, f10L:0,
            f11:1, f11L:0})

josza.subs({f00:0, f00L:1,
            f01:1, f01L:0,
            f10:1, f10L:0,
            f11:0, f11L:1})

josza.subs({f00:0, f00L:1,
            f01:0, f01L:1,
            f10:0, f10L:1,
            f11:0, f11L:1})

josza.subs({f00:1, f00L:0,
            f01:1, f01L:0,
            f10:1, f10L:0,
            f11:1, f11L:0})

josza.subs({f00:0, f00L:1,
            f01:0, f01L:1,
            f10:0, f10L:1,
            f11:1, f11L:0})