• Members 12 posts
    Sept. 4, 2021, 7:22 a.m.

    I know that fidelity is to gauge how similar two states are, so F = |<\psi|\phi>|^2. But I am a bit confused about the mathematical definition of state_fidelity in qiskit documentation.
    qiskit.org/documentation/stubs/qiskit.quantum_info.state_fidelity.html

    Besides, after I create 2 quantum circuits, and input them into state_fidelity, I got an error. Any ideas?

    from qiskit import QuantumCircuit
    from qiskit.quantum_info import state_fidelity
    
    n = 2
    qc1 = QuantumCircuit(n)
    qc2 = QuantumCircuit(n)
    
    f = state_fidelity(qc1, qc2, validate=True)
    

    Error:

    QiskitError: 'Input is not a quantum state'
    
    
  • Members 19 posts
    Sept. 4, 2021, 8:14 a.m.

    Fidelity is indeed a measure of how similar two states are. When both states are pure, it can be expressed in a simple form $F = |\langle\psi|\phi\rangle|^2$. When one or both states are not pure, fidelity needs to be calculated in terms of density operators, like documentation of state_fidelity in qiskit. This is a more general form of writing it, and it can be reduced to the simple form when both states are pure. You can read more about this in en.wikipedia.org/wiki/Fidelity_of_quantum_states

    When using state_fidelity in qiskit, you need to change the quantum circuits into state vectors using get_statevector(), before plugging them into state_fidelity. For example

    from qiskit import QuantumCircuit, execute
    from qiskit.quantum_info import state_fidelity
    from qiskit import BasicAer
    
    n = 2
    qc1 = QuantumCircuit(n)
    qc2 = QuantumCircuit(n)
    
    backend = BasicAer.get_backend('statevector_simulator')
    qc_state1 = execute(qc1, backend).result().get_statevector(qc1)
    qc_state2 = execute(qc2, backend).result().get_statevector(qc2)
    
    f = state_fidelity(qc_state1, qc_state2, validate=True)