import numpy as np
from qiskit import QuantumCircuit, BasicAer, execute
from qiskit.circuit.library import YGate
from qiskit.quantum_info import Operator, average_gate_fidelity, process_fidelity, state_fidelity

#we define a operator op_a = Ygate
op_a = Operator(YGate())

# we define also op_b=np.exp(1j / 2) * op_a which is essentially op_a but with a global phase np.exp(1j / 2)
op_b = np.exp(1j / 2) * op_a

#we run the fidelity for those gates
average_gate_fidelity(op_a,op_b)

process_fidelity(op_a, op_b)

# same here, but now with a general state
n = 1/np.sqrt(3) 
desired_state = [n,np.sqrt(1-n**2)]

qc = QuantumCircuit(1)
qc.initialize(desired_state,0)
qc.draw('mpl')

# we run it with help of a simulator
back_sv = BasicAer.get_backend('statevector_simulator')
result = execute(qc, back_sv).result()
qc_sv = result.get_statevector(qc)

#Now, we run the fidelity for those states and we see they are the same 
state_fidelity(desired_state, qc_sv) 

