from qiskit import QuantumCircuit
from qiskit.primitives import Sampler, Estimator
from qiskit.visualization import plot_histogram, plot_bloch_multivector

qc = QuantumCircuit(3)
# Apply H-gate to each qubit:
for i in range(3):
    qc.h(i)
# See the circuit:
qc.draw('mpl')

# Let's see the result
from qiskit.quantum_info import Statevector
state = Statevector.from_instruction(qc)
plot_bloch_multivector(state)




qc.draw('mpl')

from qiskit.visualization import array_to_latex
array_to_latex(state, prefix="\\text{Statevector} = ")

qc = QuantumCircuit(2)
qc.h(0)
qc.x(1)
qc.draw('mpl')



# You can similar get a unitary matrix representing the circuit as an operator by passing it to the Operator default constructor

from qiskit.quantum_info import Operator
operator = Operator(qc)

print(operator.data)


# In Jupyter Notebooks we can display this nicely using Latex.
# If not using Jupyter Notebooks you may need to remove the
# array_to_latex function and use print(unitary) instead.
from qiskit.visualization import array_to_latex
array_to_latex(operator.data, prefix="\\text{Circuit = }\n")

qc = QuantumCircuit(2)
qc.x(1)
qc.draw('mpl')

# Simulate the unitary
unitary = Operator(qc).data

# Display the results:
array_to_latex(unitary, prefix="\\text{Circuit = } ")

qc = QuantumCircuit(2)
# appkt the cnot gate

qc.cx(0,1)   # the first number represent the control qubit and the second qubit represent the target qubit

# draw it

qc.draw('mpl')

# Simulate the unitary
unitary = Operator(qc).data
# Display the results:
array_to_latex(unitary, prefix="\\text{Circuit = } ")

qc = QuantumCircuit(2)
# appkt the cnot gate

qc.cx(1,0)   # the first number represent the control qubit and the second qubit represent the target qubit

# draw it

qc.draw('mpl')

# Simulate the unitary
unitary = Operator(qc).data
# Display the results:
array_to_latex(unitary, prefix="\\text{Circuit = } ")

qc = QuantumCircuit(2)
# Apply H-gate to the first:
qc.h(0)
qc.draw('mpl')

# Let's see the result:
final_state = Statevector.from_instruction(qc)
# Print the statevector neatly:
array_to_latex(final_state, prefix="\\text{Statevector = }")

qc = QuantumCircuit(2)
# Apply H-gate to the first:
qc.h(0)
# Apply a CNOT:
qc.cx(0,1)
qc.draw('mpl')

# Let's get the result:f
final_state = Statevector.from_instruction(qc)
array_to_latex(final_state, prefix="\\text{Statevector = }")

qc.measure_all()
qc.draw('mpl')

result = sampler.run(qc).result()
counts = result.quasi_dists[0]

plot_histogram(counts)

plot_bloch_multivector(final_state)

from qiskit.visualization import plot_state_qsphere
plot_state_qsphere(final_state)