def run_circuit_and_tomography(circuit, qubits, method):
    choi_ideal = Choi(circuit).data
    qst = tomo.process_tomography_circuits(circuit, qubits)
    job = qiskit.execute(qst, Aer.get_backend('qasm_simulator'), shots=5000)
    tomo_fit = tomo.ProcessTomographyFitter(job.result(), qst)
    choi = tomo_fit.fit(method=method).data
    return (choi, choi_ideal)
def extract_choi_matrix(result: Result,
                        qpt_qcs_list: List[List[QuantumCircuit]],
                        meas_labels: List[str]) -> Choi:
    """ Estimate quantum channel from experiment.

    Args:
        result: Result of tomography experiment.
        qpt_qcs_list: Process tomography circuits.
        meas_labels: Measurement labels.

    Note:
        Need to:

            pip install cvxopt

    Yields:
        Quantum channel in Choi matrix representation.
    """
    def format_result(data_index, chunk):
        """Create new result object from partial result and marginalize."""
        new_result = deepcopy(result)
        new_result.results = []
        new_result.results.extend(result.results[data_index:data_index +
                                                 chunk])

        return marginal_counts(new_result, __reserved_registers)

    # readout error mitigation
    if len(meas_labels) > 0:
        mit_result = format_result(data_index=0, chunk=len(meas_labels))
        meas_fitter = mit.CompleteMeasFitter(mit_result,
                                             meas_labels,
                                             qubit_list=[0, 1],
                                             circlabel='mcal')
        print('readout fidelity = %.3f' % meas_fitter.readout_fidelity())
    else:
        meas_fitter = None

    # format qpt result
    qpt_results = []
    for ind, qpt_qcs in enumerate(qpt_qcs_list):
        qpt_result = format_result(data_index=len(meas_labels) +
                                   ind * len(qpt_qcs),
                                   chunk=len(qpt_qcs))
        if meas_fitter:
            qpt_results.append(meas_fitter.filter.apply(qpt_result))
        else:
            qpt_results.append(qpt_result)

    # process tomography
    for qpt_result, qpt_circuit in zip(qpt_results, qpt_qcs_list):
        process_fitter = tomo.ProcessTomographyFitter(qpt_result,
                                                      circuits=qpt_circuit)
        qpt_choi = process_fitter.fit(method='cvx', solver='CVXOPT')

        yield qpt_choi
示例#3
0
def run_circuit_and_tomography(circuit, qubits):
    job = qiskit.execute(circuit, Aer.get_backend('unitary_simulator'))
    U = job.result().get_unitary(circuit)
    choi_ideal = outer(U.ravel(order='F'))
    qst = tomo.process_tomography_circuits(circuit, qubits)
    job = qiskit.execute(qst, Aer.get_backend('qasm_simulator'), shots=5000)
    tomo_fit = tomo.ProcessTomographyFitter(job.result(), qst)
    choi_cvx = tomo_fit.fit(method='cvx').data
    choi_mle = tomo_fit.fit(method='lstsq').data
    return (choi_cvx, choi_mle, choi_ideal)