%matplotlib inline
from qiskit import QuantumCircuit, transpile
from qiskit.visualization import array_to_latex, plot_histogram
from qiskit_aer import AerSimulator
from utils import get_image_path

sim = AerSimulator()

circuit = QuantumCircuit(4,3)
circuit.x(-1)
circuit.h(range(4))
circuit.barrier()
circuit.cx(0, -1)
circuit.cx(1, -1)
#circuit.cx(2, -1)
circuit.barrier()
circuit.h(range(3))
circuit.measure(range(3), range(3))
circuit.draw('mpl', filename=get_image_path("phase_oracle_1.png"))

result = sim.run(circuit).result().get_counts()
plot_histogram(result, filename=get_image_path("result_phase_oracle_plot.png"))

from math import pi
circuit2 = QuantumCircuit(3)
circuit2.x(-1)
circuit2.ccz(2, 1, 0)
circuit2.x(-1)
circuit2.draw('mpl')

circuit_tmp = circuit2.copy()
circuit_tmp.save_unitary()
unitary = sim.run(transpile(circuit_tmp, sim)).result().get_unitary()
array_to_latex(unitary, max_size=1000)