qiskit/tools/apps/optimization.py
def eval_hamiltonian(hamiltonian, input_circuit, shots, device):
    """Calculates the average value of a Hamiltonian on a state created by the
     input circuit

    Args:
        hamiltonian (array or matrix or list): a representation of the
            Hamiltonian or observables to be measured. If it is a list, it is
            a list of Pauli operators grouped into tpb sets.
        input_circuit (QuantumCircuit): input circuit.
        shots (int): number of shots considered in the averaging. If 1 the
            averaging is exact.
        device (str): the backend used to run the simulation.
    Returns:
        float: Average value of the Hamiltonian or observable.
    """
    energy = 0
    circuits = []

    if 'statevector' in device:
        circuits.append(input_circuit)
        # Hamiltonian is not a pauli_list grouped into tpb sets
        if not isinstance(hamiltonian, list):
            result = execute(circuits, device, shots=shots).result()
            statevector = result.get_statevector()
            # Diagonal Hamiltonian represented by 1D array
            if (hamiltonian.shape[0] == 1 or
                    np.shape(np.shape(np.array(hamiltonian))) == (1,)):
                energy = np.sum(hamiltonian * np.absolute(statevector) ** 2)
            # Hamiltonian represented by square matrix
            elif hamiltonian.shape[0] == hamiltonian.shape[1]:
                energy = np.inner(np.conjugate(statevector),
                                  np.dot(hamiltonian, statevector))
        # Hamiltonian represented by a Pauli list
        else:
            # Execute trial circuit with final rotations for each Pauli in
            # hamiltonian and store from circuits[1] on
            n_qubits = input_circuit.regs['q'].size
            q = QuantumRegister(n_qubits, "q")
            i = 1
            for p in hamiltonian:
                circuits.append(copy.deepcopy(input_circuit))
                for j in range(n_qubits):
                    if p[1].v[j] == 0 and p[1].w[j] == 1:
                        circuits[i].x(q[j])
                    elif p[1].v[j] == 1 and p[1].w[j] == 0:
                        circuits[i].z(q[j])
                    elif p[1].v[j] == 1 and p[1].w[j] == 1:
                        circuits[i].y(q[j])
                i += 1
            result = execute(circuits, device, shots=shots).result()
            # no Pauli final rotations
            statevector_0 = result.get_statevector(circuits[0])
            i = 1
            for p in hamiltonian:
                statevector_i = result.get_statevector(circuits[i])
                # inner product with final rotations of (i-1)-th Pauli
                energy += p[0] * np.inner(np.conjugate(statevector_0),
                                          statevector_i)
                i += 1
    # finite number of shots and hamiltonian grouped in tpb sets
    else:
        n = int(len(hamiltonian[0][0][1].v))
        q = QuantumRegister(n, "q")
        c = ClassicalRegister(n, "c")
        i = 0
        for tpb_set in hamiltonian:
            circuits.append(copy.deepcopy(input_circuit))
            for j in range(n):
                # Measure X
                if tpb_set[0][1].v[j] == 0 and tpb_set[0][1].w[j] == 1:
                    circuits[i].h(q[j])
                # Measure Y
                elif tpb_set[0][1].v[j] == 1 and tpb_set[0][1].w[j] == 1:
                    circuits[i].s(q[j]).inverse()
                    circuits[i].h(q[j])
                circuits[i].measure(q[j], c[j])
            i += 1
        result = execute(circuits, device, shots=shots).result()
        for j, _ in enumerate(hamiltonian):
            for k, _ in enumerate(hamiltonian[j]):
                energy += hamiltonian[j][k][0] *\
                    measure_pauli_z(result.get_counts(circuits[j]),
                                    hamiltonian[j][k][1])

    return energy, circuits
Calculates the average value of a Hamiltonian on a state created by the
     input circuit

    Args:
        hamiltonian (array or matrix or list): a representation of the
            Hamiltonian or observables to be measured. If it is a list, it is
            a list of Pauli operators grouped into tpb sets.
        input_circuit (QuantumCircuit): input circuit.
        shots (int): number of shots considered in the averaging. If 1 the
            averaging is exact.
        device (str): the backend used to run the simulation.
    Returns:
        float: Average value of the Hamiltonian or observable.
    
Calculates the average value of a Hamiltonian on a state created by the
input circuit
Calculates the average value of a Hamiltonian on a state created by the input circuit
def eval_hamiltonian(hamiltonian, input_circuit, shots, device):
    energy = 0
    circuits = []
    if 'statevector' in device:
        circuits.append(input_circuit)
        if not isinstance(hamiltonian, list):
            result = execute(circuits, device, shots=shots).result()
            statevector = result.get_statevector()
            if (hamiltonian.shape[0] == 1 or
                    np.shape(np.shape(np.array(hamiltonian))) == (1,)):
                energy = np.sum(hamiltonian * np.absolute(statevector) ** 2)
            elif hamiltonian.shape[0] == hamiltonian.shape[1]:
                energy = np.inner(np.conjugate(statevector),
                                  np.dot(hamiltonian, statevector))
        else:
            n_qubits = input_circuit.regs['q'].size
            q = QuantumRegister(n_qubits, "q")
            i = 1
            for p in hamiltonian:
                circuits.append(copy.deepcopy(input_circuit))
                for j in range(n_qubits):
                    if p[1].v[j] == 0 and p[1].w[j] == 1:
                        circuits[i].x(q[j])
                    elif p[1].v[j] == 1 and p[1].w[j] == 0:
                        circuits[i].z(q[j])
                    elif p[1].v[j] == 1 and p[1].w[j] == 1:
                        circuits[i].y(q[j])
                i += 1
            result = execute(circuits, device, shots=shots).result()
            statevector_0 = result.get_statevector(circuits[0])
            i = 1
            for p in hamiltonian:
                statevector_i = result.get_statevector(circuits[i])
                energy += p[0] * np.inner(np.conjugate(statevector_0),
                                          statevector_i)
                i += 1
    else:
        n = int(len(hamiltonian[0][0][1].v))
        q = QuantumRegister(n, "q")
        c = ClassicalRegister(n, "c")
        i = 0
        for tpb_set in hamiltonian:
            circuits.append(copy.deepcopy(input_circuit))
            for j in range(n):
                if tpb_set[0][1].v[j] == 0 and tpb_set[0][1].w[j] == 1:
                    circuits[i].h(q[j])
                elif tpb_set[0][1].v[j] == 1 and tpb_set[0][1].w[j] == 1:
                    circuits[i].s(q[j]).inverse()
                    circuits[i].h(q[j])
                circuits[i].measure(q[j], c[j])
            i += 1
        result = execute(circuits, device, shots=shots).result()
        for j, _ in enumerate(hamiltonian):
            for k, _ in enumerate(hamiltonian[j]):
                energy += hamiltonian[j][k][0] *\
                    measure_pauli_z(result.get_counts(circuits[j]),
                                    hamiltonian[j][k][1])
    return energy, circuits
def eval_hamiltonian ( hamiltonian , input_circuit , shots , device ) : energy = 0 circuits = [ ] if 'statevector' in device : circuits . append ( input_circuit ) if not isinstance ( hamiltonian , list ) : result = execute ( circuits , device , shots = shots ) . result ( ) statevector = result . get_statevector ( ) if ( hamiltonian . shape [ 0 ] == 1 or np . shape ( np . shape ( np . array ( hamiltonian ) ) ) == ( 1 , ) ) : energy = np . sum ( hamiltonian * np . absolute ( statevector ) ** 2 ) elif hamiltonian . shape [ 0 ] == hamiltonian . shape [ 1 ] : energy = np . inner ( np . conjugate ( statevector ) , np . dot ( hamiltonian , statevector ) ) else : n_qubits = input_circuit . regs [ 'q' ] . size q = QuantumRegister ( n_qubits , "q" ) i = 1 for p in hamiltonian : circuits . append ( copy . deepcopy ( input_circuit ) ) for j in range ( n_qubits ) : if p [ 1 ] . v [ j ] == 0 and p [ 1 ] . w [ j ] == 1 : circuits [ i ] . x ( q [ j ] ) elif p [ 1 ] . v [ j ] == 1 and p [ 1 ] . w [ j ] == 0 : circuits [ i ] . z ( q [ j ] ) elif p [ 1 ] . v [ j ] == 1 and p [ 1 ] . w [ j ] == 1 : circuits [ i ] . y ( q [ j ] ) i += 1 result = execute ( circuits , device , shots = shots ) . result ( ) statevector_0 = result . get_statevector ( circuits [ 0 ] ) i = 1 for p in hamiltonian : statevector_i = result . get_statevector ( circuits [ i ] ) energy += p [ 0 ] * np . inner ( np . conjugate ( statevector_0 ) , statevector_i ) i += 1 else : n = int ( len ( hamiltonian [ 0 ] [ 0 ] [ 1 ] . v ) ) q = QuantumRegister ( n , "q" ) c = ClassicalRegister ( n , "c" ) i = 0 for tpb_set in hamiltonian : circuits . append ( copy . deepcopy ( input_circuit ) ) for j in range ( n ) : if tpb_set [ 0 ] [ 1 ] . v [ j ] == 0 and tpb_set [ 0 ] [ 1 ] . w [ j ] == 1 : circuits [ i ] . h ( q [ j ] ) elif tpb_set [ 0 ] [ 1 ] . v [ j ] == 1 and tpb_set [ 0 ] [ 1 ] . w [ j ] == 1 : circuits [ i ] . s ( q [ j ] ) . inverse ( ) circuits [ i ] . h ( q [ j ] ) circuits [ i ] . measure ( q [ j ] , c [ j ] ) i += 1 result = execute ( circuits , device , shots = shots ) . result ( ) for j , _ in enumerate ( hamiltonian ) : for k , _ in enumerate ( hamiltonian [ j ] ) : energy += hamiltonian [ j ] [ k ] [ 0 ] * measure_pauli_z ( result . get_counts ( circuits [ j ] ) , hamiltonian [ j ] [ k ] [ 1 ] ) return energy , circuits
Calculates the average value of a Hamiltonian on a state created by the
input circuit
Calculates the average value of a Hamiltonian on a state created by the input circuit
"""Calculates the average value of a Hamiltonian on a state created by the
     input circuit

    Args:
        hamiltonian (array or matrix or list): a representation of the
            Hamiltonian or observables to be measured. If it is a list, it is
            a list of Pauli operators grouped into tpb sets.
        input_circuit (QuantumCircuit): input circuit.
        shots (int): number of shots considered in the averaging. If 1 the
            averaging is exact.
        device (str): the backend used to run the simulation.
    Returns:
        float: Average value of the Hamiltonian or observable.
    """ # Hamiltonian is not a pauli_list grouped into tpb sets # Diagonal Hamiltonian represented by 1D array # Hamiltonian represented by square matrix # Hamiltonian represented by a Pauli list # Execute trial circuit with final rotations for each Pauli in # hamiltonian and store from circuits[1] on # no Pauli final rotations # inner product with final rotations of (i-1)-th Pauli # finite number of shots and hamiltonian grouped in tpb sets # Measure X # Measure Y
Average value of the Hamiltonian or observable.
Average value of the Hamiltonian or observable .
float
hamiltonian
a representation of the
Hamiltonian or observables to be measured. If it is a list, it is
a list of Pauli operators grouped into tpb sets.
a representation of the Hamiltonian or observables to be measured . If it is a list it is a list of Pauli operators grouped into tpb sets .
input_circuit
None
shots
number of shots considered in the averaging. If 1 the
averaging is exact.
number of shots considered in the averaging . If 1 the averaging is exact .
device
the backend used to run the simulation.
the backend used to run the simulation .