def run_circuit_and_tomography(circuit, qubits, method='lstsq'):
    psi = Statevector.from_instruction(circuit)
    qst = tomo.state_tomography_circuits(circuit, qubits)
    job = qiskit.execute(qst, Aer.get_backend('qasm_simulator'), shots=5000)
    tomo_fit = tomo.StateTomographyFitter(job.result(), qst)
    rho = tomo_fit.fit(method=method)
    return (rho, psi)
Exemple #2
0
    def test_identity2(self, vector):
        # Define the linear system
        vector = np.array(vector)
        matrix = np.identity(2)
        system = LinearSystemSolverQSVE(matrix, vector, nprecision_bits=5, cval=0.1)

        # Get the circuit and registers
        circuit, _, _, col_register, _ = system.create_circuit(return_registers=True)

        # Test and debug
        print("Current vector is:", vector)
        print("Doing state tomography on col_register, which has {} qubit(s).".format(len(col_register)))
        tomo_circuits = tomography.state_tomography_circuits(circuit, col_register)
        print("There are {} tomography circuits. Running them now...".format(len(tomo_circuits)))
        job = execute(tomo_circuits, BasicAer.get_backend("qasm_simulator"), shots=10000)
        print("Finished running tomography circuits. Now fitting density matrix.")
        fitter = tomography.StateTomographyFitter(job.result(), tomo_circuits)
        rho = fitter.fit()
        print("Density matrix:\n", rho)

        expected = np.outer(vector, vector)
        expected /= np.trace(expected)

        print("Expected density matrix:\n", expected)

        self.assertTrue(np.allclose(rho, expected, atol=1e-2))
def run_circuit_and_tomography(circuit, qubits):
    job = qiskit.execute(circuit, Aer.get_backend('statevector_simulator'))
    psi = job.result().get_statevector(circuit)
    qst = tomo.state_tomography_circuits(circuit, qubits)
    job = qiskit.execute(qst, Aer.get_backend('qasm_simulator'), shots=5000)
    tomo_fit = tomo.StateTomographyFitter(job.result(), qst)
    rho_cvx = tomo_fit.fit(method='cvx')
    rho_mle = tomo_fit.fit(method='lstsq')
    return (rho_cvx, rho_mle, psi)
Exemple #4
0
 def time_state_tomography_cat(self, n_qubits):
     qr = qiskit.QuantumRegister(n_qubits, 'qr')
     circ = qiskit.QuantumCircuit(qr, name='cat')
     circ.h(qr[0])
     for i in range(1, n_qubits):
         circ.cx(qr[0], qr[i])
     psi = qiskit.execute(circ, self.sv_backend).result().get_statevector()
     qst_circ = tomo.state_tomography_circuits(circ, qr)
     tomo_result = qiskit.execute(qst_circ, self.qasm_backend,
                                  shots=5000).result()
     rho = tomo.StateTomographyFitter(tomo_result, qst_circ).fit()
     state_fidelity(psi, rho)
Exemple #5
0
 def time_state_tomography_bell(self, n_qubits):
     qr = qiskit.QuantumRegister(2)
     bell = qiskit.QuantumCircuit(qr)
     bell.h(qr[0])
     bell.cx(qr[0], qr[1])
     psi_bell = qiskit.execute(
         bell, self.sv_backend).result().get_statevector(bell)
     qr_full = qiskit.QuantumRegister(n_qubits)
     bell = qiskit.QuantumCircuit(qr_full)
     bell.h(qr_full[n_qubits - 2])
     bell.cx(qr_full[n_qubits - 2], qr_full[n_qubits - 1])
     qst_bell = tomo.state_tomography_circuits(
         bell, [qr_full[n_qubits - 2], qr_full[n_qubits - 1]])
     job = qiskit.execute(qst_bell, self.qasm_backend, shots=5000)
     rho_bell = tomo.StateTomographyFitter(job.result(), qst_bell).fit()
     state_fidelity(psi_bell, rho_bell)
    def test_fitter_string_input(self):
        q3 = QuantumRegister(3)
        bell = QuantumCircuit(q3)
        bell.h(q3[0])
        bell.cx(q3[0], q3[1])
        bell.cx(q3[1], q3[2])

        qst = tomo.state_tomography_circuits(bell, q3)
        qst_names = [circ.name for circ in qst]
        job = qiskit.execute(qst, Aer.get_backend('qasm_simulator'),
                             shots=5000)
        tomo_fit = tomo.StateTomographyFitter(job.result(), qst_names)
        rho = tomo_fit.fit(method=self.method)
        psi = Statevector.from_instruction(bell)
        F_bell = state_fidelity(psi, rho, validate=False)
        self.assertAlmostEqual(F_bell, 1, places=1)
    def test_split_job(self):
        q3 = QuantumRegister(3)
        bell = QuantumCircuit(q3)
        bell.h(q3[0])
        bell.cx(q3[0], q3[1])
        bell.cx(q3[1], q3[2])

        psi = Statevector.from_instruction(bell)
        qst = tomo.state_tomography_circuits(bell, q3)
        qst1 = qst[:len(qst) // 2]
        qst2 = qst[len(qst) // 2:]

        backend = Aer.get_backend('qasm_simulator')
        job1 = qiskit.execute(qst1, backend, shots=5000)
        job2 = qiskit.execute(qst2, backend, shots=5000)

        tomo_fit = tomo.StateTomographyFitter([job1.result(), job2.result()], qst)

        rho_mle = tomo_fit.fit(method='lstsq')
        F_bell_mle = state_fidelity(psi, rho_mle, validate=False)
        self.assertAlmostEqual(F_bell_mle, 1, places=1)
Exemple #8
0
noise_model2.add_all_qubit_quantum_error(
    thermal_relaxation_error(t1, t2, 2 * gate1Q), 'u3')
noise_model2.add_all_qubit_quantum_error(
    thermal_relaxation_error(t1, t2, gate2Q).tensor(
        thermal_relaxation_error(t1, t2, gate2Q)), 'cx')

# End Noise configuration

# Start state tomography - without noise

qst_deutsch = tomo.state_tomography_circuits(circuit, q5)
job = qiskit.execute(qst_deutsch, backend_sim, shots=5000)
results = job.result()
print("results:")
print(results.get_counts(0))
statefit = tomo.StateTomographyFitter(results, qst_deutsch)
p, M, weights = statefit._fitter_data(True, 0.5)
M_dg = np.conj(M).T
linear_inversion_matrix = np.linalg.inv(M_dg @ M) @ M_dg
rho_deutsch = linear_inversion_matrix @ p
rho_deutsch = np.reshape(rho_deutsch, (2**num_qubits, 2**num_qubits))

job = qiskit.execute(circuit, Aer.get_backend('statevector_simulator'))
psi_deutsch_clear = job.result().get_statevector(circuit)
print("Deutsch state vector (without noise): ")
print(psi_deutsch_clear)

F_deutsch = state_fidelity(psi_deutsch_clear, rho_deutsch, validate=False)
print('Fit Fidelity linear inversion =', F_deutsch)

rho_mle_deutsch = statefit.fit(method='lstsq')