示例#1
0
 def add_tomo_circuits(self, circ):
     if not self.use_quantum_program:
         # Construct state tomography set for measurement of qubits in the
         # register
         qr = next(iter(circ.qregs))
         cr = next(iter(circ.cregs))
         tomo_set = tomo.state_tomography_set(list(range(qr.size)))
         # Add the state tomography measurement circuits
         tomo_circuits = tomo.create_tomography_circuits(
             circ, qr, cr, tomo_set)
         return tomo_set, tomo_circuits
     if self.use_quantum_program:
         # Construct state tomography set for measurement of qubits in the
         # register
         qr_name = list(circ.get_quantum_register_names())[0]
         cr_name = list(circ.get_classical_register_names())[0]
         qr = circ.get_quantum_register(qr_name)
         cr = circ.get_classical_register(cr_name)
         tomo_set = tomo.state_tomography_set(list(range(qr.size)))
         # Add the state tomography measurement circuits to the Quantum
         # Program
         tomo_circuits = tomo.create_tomography_circuits(
             circ, qr, cr, tomo_set)
         return circ, tomo_set, tomo_circuits
     raise Exception
示例#2
0
 def test_state_tomography_1qubit(self):
     # Tomography set
     tomo_set = tomo.state_tomography_set([0])
     # Get test circuits
     circuits, qr, cr = _test_circuits_1qubit()
     # Test simulation and fitting
     shots = 2000
     threshold = 1e-2
     rho = _tomography_test_data(circuits['Zp'], qr, cr, tomo_set, shots)
     self.assertTrue(_tomography_test_fit(rho, [1, 0], threshold))
     rho = _tomography_test_data(circuits['Zm'], qr, cr, tomo_set, shots)
     self.assertTrue(_tomography_test_fit(rho, [0, 1], threshold))
     rho = _tomography_test_data(circuits['Xp'], qr, cr, tomo_set, shots)
     self.assertTrue(
         _tomography_test_fit(rho, [1 / np.sqrt(2), 1 / np.sqrt(2)],
                              threshold))
     rho = _tomography_test_data(circuits['Xm'], qr, cr, tomo_set, shots)
     self.assertTrue(
         _tomography_test_fit(rho, [1 / np.sqrt(2), -1 / np.sqrt(2)],
                              threshold))
     rho = _tomography_test_data(circuits['Yp'], qr, cr, tomo_set, shots)
     self.assertTrue(
         _tomography_test_fit(rho, [1 / np.sqrt(2), 1j / np.sqrt(2)],
                              threshold))
     rho = _tomography_test_data(circuits['Ym'], qr, cr, tomo_set, shots)
     self.assertTrue(
         _tomography_test_fit(rho, [1 / np.sqrt(2), -1j / np.sqrt(2)],
                              threshold))
 def test_state_tomography_1qubit(self):
     # Tomography set
     tomo_set = tomo.state_tomography_set([0])
     # Get test circuits
     qp, qr, cr = _test_circuits_1qubit()
     # Test simulation and fitting
     shots = 2000
     threshold = 1e-2
     rho = _tomography_test_data(qp, 'Zp', qr, cr, tomo_set, shots)
     self.assertTrue(_tomography_test_fit(rho, [1, 0], threshold))
     rho = _tomography_test_data(qp, 'Zm', qr, cr, tomo_set, shots)
     self.assertTrue(_tomography_test_fit(rho, [0, 1], threshold))
     rho = _tomography_test_data(qp, 'Xp', qr, cr, tomo_set, shots)
     self.assertTrue(
         _tomography_test_fit(rho, [1 / np.sqrt(2), 1 / np.sqrt(2)],
                              threshold))
     rho = _tomography_test_data(qp, 'Xm', qr, cr, tomo_set, shots)
     self.assertTrue(
         _tomography_test_fit(rho, [1 / np.sqrt(2), -1 / np.sqrt(2)],
                              threshold))
     rho = _tomography_test_data(qp, 'Yp', qr, cr, tomo_set, shots)
     self.assertTrue(
         _tomography_test_fit(rho, [1 / np.sqrt(2), 1j / np.sqrt(2)],
                              threshold))
     rho = _tomography_test_data(qp, 'Ym', qr, cr, tomo_set, shots)
     self.assertTrue(
         _tomography_test_fit(rho, [1 / np.sqrt(2), -1j / np.sqrt(2)],
                              threshold))
示例#4
0
    def _state_tomography(self):
        """The state tomography.

        The HHL result gets extracted via state tomography. Available for
        qasm simulator and real hardware backends.
        """
        # Preparing the state tomography circuits
        c = ClassicalRegister(self._num_q)
        self._circuit.add_register(c)
        tomo_qbits = list(range(self._num_q))
        tomo_set = tomo.state_tomography_set(tomo_qbits)
        tomo_circuits = \
            tomo.create_tomography_circuits(self._circuit,
                                            self._io_register,
                                            c, tomo_set)
        # Handling the results
        result = self._quantum_instance.execute(tomo_circuits)
        probs = []
        for circ in tomo_circuits:
            counts = result.get_counts(circ)
            s, f = 0, 0
            for k, v in counts.items():
                if k[-1] == "1":
                    s += v
                else:
                    f += v
            probs.append(s/(f+s))
        self._ret["probability_result"] = probs
        # Filtering the tomo data for valid results, i.e. c0==1
        tomo_data = self._tomo_postselect(result, self._circuit.name,
                                          tomo_set, self._success_bit)
        # Fitting the tomography data
        rho_fit = tomo.fit_tomography_data(tomo_data)
        vec = rho_fit[:, 0]/np.sqrt(rho_fit[0, 0])
        self._hhl_results(vec)
def add_tomo_circuits(circ):
    # Construct state tomography set for measurement of qubits in the register
    qr = next(iter(circ.get_qregs().values()))
    cr = next(iter(circ.get_cregs().values()))
    tomo_set = tomo.state_tomography_set(list(range(qr.size)))

    # Add the state tomography measurement circuits
    tomo_circuits = tomo.create_tomography_circuits(circ, qr, cr, tomo_set)

    return tomo_set, tomo_circuits
def add_tomo_circuits(qp):
    # Construct state tomography set for measurement of qubits in the register
    qr_name = list(qp.get_quantum_register_names())[0]
    cr_name = list(qp.get_classical_register_names())[0]
    qr = qp.get_quantum_register(qr_name)
    cr = qp.get_classical_register(cr_name)
    tomo_set = tomo.state_tomography_set(list(range(qr.size)))

    # Add the state tomography measurement circuits to the Quantum Program
    tomo_circuits = tomo.create_tomography_circuits(qp, 'prep', qr, cr, tomo_set)

    return qp, tomo_set, tomo_circuits
def add_tomo_circuits(qp):
    # Construct state tomography set for measurement of qubits in the register
    qr_name = list(qp.get_quantum_register_names())[0]
    cr_name = list(qp.get_classical_register_names())[0]
    qr = qp.get_quantum_register(qr_name)
    cr = qp.get_classical_register(cr_name)
    tomo_set = tomo.state_tomography_set(list(range(qr.size)))

    # Add the state tomography measurement circuits to the Quantum Program
    tomo_circuits = tomo.create_tomography_circuits(qp, 'prep', qr, cr, tomo_set)

    return qp, tomo_set, tomo_circuits
示例#8
0
 def test_state_tomography_2qubit(self):
     # Tomography set
     tomo_set = tomo.state_tomography_set([0, 1])
     # Get test circuits
     circuits, qr, cr = _test_circuits_2qubit()
     shots = 2000
     threshold = 1e-2
     # Test simulation and fitting
     rho = _tomography_test_data(circuits['Bell'], qr, cr, tomo_set, shots)
     self.assertTrue(
         _tomography_test_fit(rho, [1 / np.sqrt(2), 0, 0, 1 / np.sqrt(2)],
                              threshold))
     rho = _tomography_test_data(circuits['X1Id0'], qr, cr, tomo_set, shots)
     self.assertTrue(_tomography_test_fit(rho, [0, 0, 1, 0], threshold))
 def test_state_tomography_2qubit(self):
     # Tomography set
     tomo_set = tomo.state_tomography_set([0, 1])
     # Get test circuits
     qp, qr, cr = _test_circuits_2qubit()
     shots = 2000
     threshold = 1e-2
     # Test simulation and fitting
     rho = _tomography_test_data(qp, 'Bell', qr, cr, tomo_set, shots)
     self.assertTrue(
         _tomography_test_fit(rho, [1 / np.sqrt(2), 0, 0, 1 / np.sqrt(2)],
                              threshold))
     rho = _tomography_test_data(qp, 'X1Id0', qr, cr, tomo_set, shots)
     self.assertTrue(_tomography_test_fit(rho, [0, 0, 1, 0], threshold))
示例#10
0
 def test_state_tomography_set_default(self):
     pauli_set = tomo.state_tomography_set([0], meas_basis='Pauli')
     default_set = tomo.state_tomography_set([0])
     self.assertEqual(pauli_set['circuits'], default_set['circuits'])
示例#11
0
        print(c, x.get("status"))
        if x.get("status") != "COMPLETED":
            b = False
        xs += x["qasms"]
        c += 1
    if not b:
        sys.exit(1)
    return Result({"result": xs, "status": "DONE"}, qobj)


# tomo_set = tomo.state_tomography_set(list(range(4)))
# tomo_circuits = tomo.create_tomography_circuits(qp, 'tomo', qr, cr, tomo_set)
#
# res1 = qp.execute(tomo_circuits, backend="local_qiskit_simulator", shots=1024)

tomo_set = tomo.state_tomography_set(list(range(int(n / 2))))

if sys.argv[1] == "run":
    backend = "ibmqx5"
    tomo_circuits = tomo.create_tomography_circuits(qp, 'tomo', qr, cr,
                                                    tomo_set)
    qobj = qp.compile(tomo_circuits, backend=backend, shots=1024)

    qasms = split_qobj(qobj, M=1)
    run_splitted_qasms(qasms, qobj, backend)
    sys.exit(0)
elif sys.argv[1] == "simulate":
    tomo_circuits = tomo.create_tomography_circuits(qp, 'tomo', qr, cr,
                                                    tomo_set)
    qobj = qp.compile(tomo_circuits,
                      backend="local_qiskit_simulator",
 def test_state_tomography_set_default(self):
     pauli_set = tomo.state_tomography_set([0], meas_basis='Pauli')
     default_set = tomo.state_tomography_set([0])
     self.assertEqual(pauli_set['circuits'], default_set['circuits'])
# quantum circuit to make an entangled Bell state
bell = QuantumCircuit(qr, cr, name='bell')
bell.h(qr[1])
bell.cx(qr[1], qr[0])

#print(qiskit.available_backends())
job = qiskit.execute(bell, backend='local_statevector_simulator')
bell_psi = job.result().get_statevector(bell)
bell_rho = outer(
    bell_psi)  # construct the density matrix from the state vector

#plot the state
plot_state(bell_rho, 'paulivec')

# Construct state tomography set for measurement of qubits [0, 1] in the Pauli basis
bell_tomo_set = tomo.state_tomography_set([0, 1])

# Create a quantum program containing the state preparation circuit
Q_program = QuantumProgram()
Q_program.add_circuit('bell', bell)

# Add the state tomography measurement circuits to the Quantum Program
bell_tomo_circuit_names = tomo.create_tomography_circuits(
    Q_program, 'bell', qr, cr, bell_tomo_set)

print('Created State tomography circuits:')
for name in bell_tomo_circuit_names:
    print(name)

# Use the local simulator# Use t
backend = 'local_qasm_simulator'