def test_outer(self): v_z = [1, 0] v_y = [1, 1j] rho_z = [[1, 0], [0, 0]] rho_y = [[1, -1j], [1j, 1]] op_zy = [[1, -1j], [0, 0]] op_yz = [[1, 0], [1j, 0]] test_pass = np.linalg.norm(outer(v_z) - rho_z) == 0 and \ np.linalg.norm(outer(v_y) - rho_y) == 0 and \ np.linalg.norm(outer(v_y, v_z) - op_yz) == 0 and \ np.linalg.norm(outer(v_z, v_y) - op_zy) == 0 self.assertTrue(test_pass)
def test_outer(self): v_z = [1, 0] v_y = [1, 1j] rho_z = [[1, 0], [0, 0]] rho_y = [[1, -1j], [1j, 1]] op_zy = [[1, -1j], [0, 0]] op_yz = [[1, 0], [1j, 0]] test_pass = (np.linalg.norm(outer(v_z) - rho_z) == 0 and np.linalg.norm(outer(v_y) - rho_y) == 0 and np.linalg.norm(outer(v_y, v_z) - op_yz) == 0 and np.linalg.norm(outer(v_z, v_y) - op_zy) == 0) self.assertTrue(test_pass)
def __wizard(rho, epsilon=None): """ Returns the nearest positive semidefinite operator to an operator. This method is based on reference [1]. It constrains positivity by setting negative eigenvalues to zero and rescaling the positive eigenvalues. Args: rho (array_like): the input operator. epsilon(float or None): threshold (>=0) for truncating small eigenvalues values to zero. Returns: numpy.array: A positive semidefinite numpy array. """ if epsilon is None: epsilon = 0. # default value dim = len(rho) rho_wizard = np.zeros([dim, dim]) v, w = np.linalg.eigh(rho) # v eigenvecrors v[0] < v[1] <... for j in range(dim): if v[j] < epsilon: tmp = v[j] v[j] = 0. # redistribute loop x = 0. for k in range(j + 1, dim): x += tmp / (dim - (j + 1)) v[k] = v[k] + tmp / (dim - (j + 1)) for j in range(dim): rho_wizard = rho_wizard + v[j] * outer(w[:, j]) return rho_wizard
def __wizard(rho, epsilon=None): """ Returns the nearest postitive semidefinite operator to an operator. This method is based on reference [1]. It constrains positivity by setting negative eigenvalues to zero and rescaling the positive eigenvalues. Args: rho (array_like): the input operator. epsilon(float or None): threshold (>=0) for truncating small eigenvalues values to zero. Returns: numpy.array: A positive semidefinite numpy array. """ if epsilon is None: epsilon = 0. # default value dim = len(rho) rho_wizard = np.zeros([dim, dim]) v, w = np.linalg.eigh(rho) # v eigenvecrors v[0] < v[1] <... for j in range(dim): if v[j] < epsilon: tmp = v[j] v[j] = 0. # redistribute loop x = 0. for k in range(j + 1, dim): x += tmp / (dim - (j + 1)) v[k] = v[k] + tmp / (dim - (j + 1)) for j in range(dim): rho_wizard = rho_wizard + v[j] * outer(w[:, j]) return rho_wizard
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)
def concurrence(state): """Calculate the concurrence. Args: state (np.array): a quantum state (1x4 array) or a density matrix (4x4 array) Returns: float: concurrence. Raises: Exception: if attempted on more than two qubits. """ rho = np.array(state) if rho.ndim == 1: rho = outer(state) if len(state) != 4: raise Exception("Concurrence is only defined for more than two qubits") YY = np.fliplr(np.diag([-1, 1, 1, -1])) A = rho.dot(YY).dot(rho.conj()).dot(YY) w = la.eigvals(A) w = np.sort(np.real(w)) w = np.sqrt(np.maximum(w, 0)) return max(0.0, w[-1] - np.sum(w[0:-1]))
import numpy as np from qiskit import QuantumCircuit, QuantumProgram import Qconfig import qiskit.tools.qcvv.tomography as tomography from qiskit.tools.visualization import plot_state, plot_histogram from qiskit.tools.qi.qi import state_fidelity, concurrence, purity, outer QProgram = QuantumProgram() QProgram.set_api(Qconfig.APItoken, Qconfig.config['url']) qr = QProgram.create_quantum_register('qr',2) cr = QProgram.create_classical_register('cr',2) bell = QProgram.create_circuit('bell', [qr], [cr]) bell.h(qr[0]) bell.cx(qr[0], qr[1]) bell_result = QProgram.execute(['bell'], backend = 'local_qasm_simulator', shots = 1) bell_psi = bell_result.get_data('bell') ['quantum_state'] bell_rho = outer(bell_psi) plot_state(bell_rho, 'paulivec')
from qiskit.tools.visualization import plot_state, plot_histogram from qiskit.tools.qi.qi import state_fidelity, concurrence, purity, outer # Create a 2-qubit quantum register# Creat qr = QuantumRegister(2) cr = ClassicalRegister(2) # 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)
from qiskit import QuantumCircuit, QuantumProgram import Qconfig import qiskit.tools.qcvv.tomography as tomo import numpy as np from qiskit.tools.visualization import plot_state, plot_histogram from qiskit.tools.qi.qi import state_fidelity, concurrence, purity, outer p = QuantumProgram() p.set_api(Qconfig.APItoken, Qconfig.config['url']) qr = p.create_quantum_register('qr', 2) cr = p.create_classical_register('cr', 2) # quantum circuit to make an entangled bell state bell = p.create_circuit('bell', [qr], [cr]) bell.h(qr[0]) bell.cx(qr[0], qr[1]) bell_result = p.execute(['bell'], backend='local_qasm_simulator', shots=1) bell_psi = bell_result.get_data('bell')['quantum_state'] bell_rho = outer(bell_psi) # construct the density matrix from the state vector #plot_state(bell_rho,'paulivec') rho_mixed = np.array([[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,1]])/2 plot_state(rho_mixed, 'paulivec')