def circuit_statevector_solver(circuit): backend = Aer.get_backend('statevector_simulator') result = execute(circuit, backend=backend).result() statevector = result.get_statevector(circuit) print("State vector: {}".format(statevector)) fig = plot_state_city(statevector) fig.savefig("/home/agustinsilva447/Github/Reinforcement-Learning/Prueba 1/state.png") return statevector
def qiskit(): # Plotting Single Bloch Sphere plot_bloch_vector([0, 1, 0], title='Bloch Sphere') # Building Quantum Circuit to use for multiqubit systems qc = QuantumCircuit(2) qc.h(0) qc.cx(0, 1) # Plotting Multi Bloch System state = Statevector.from_instruction(qc) plot_bloch_multivector(state, title="New Bloch Multivector") # Plotting Bloch City Scape plot_state_city(state, color=['midnightblue', 'midnightblue'], title="New State City") # Plotting Bloch Pauli Vectors plot_state_paulivec(state, color='midnightblue', title="New PauliVec plot")
def plot_SC(state_0, state_1, filename = None): # state_0 : statevector of denoised state # state_1 : statevector of noisy state # filename : whether to save figure and figure name fig = plt.figure(figsize=(15,10)) plt.title("States city of noisy (top) and denoised (bottom) states. We used a [2,1,2] QAE, and 200 GHZ states affected by the QDC with noise strength $p = 0.4$. \n The states city are respectively the average initial state vector and the average density matrix output by the QAE.") plt.axis("off") ax1 = fig.add_subplot(2, 2, 1, projection='3d') ax2 = fig.add_subplot(2, 2, 2, projection='3d') ax3 = fig.add_subplot(2, 2, 3, projection='3d') ax4 = fig.add_subplot(2, 2, 4, projection='3d') plot_state_city(state_1, color = ['crimson', 'crimson'], ax_real = ax1, ax_imag = ax2) plot_state_city(state_0, color = ['crimson', 'crimson'], ax_real = ax3, ax_imag = ax4) ax1.set_zlim(0,0.5) ax2.set_zlim(0,0.5) ax3.set_zlim(0,0.5) ax4.set_zlim(0,0.5) if filename != None: plt.savefig(filename + '.pdf', bbox_inches = 'tight')
def plot(theoretical_rho_GHZ_list, rho_GHZ_list, theoretical_rho_T_list, rho_T_list): try: os.mkdir('state_cities') except FileExistsError: pass c = ['#ba0c2f', '#0cba97'] a = 0.8 bbox = Bbox(np.array([[2.3, 0], [13.7, 4.2]])) for i in range(len(theoretical_rho_GHZ_list)): visual.plot_state_city( theoretical_rho_GHZ_list[i], color=c, alpha=a).savefig( 'state_cities/Theo_GHZ_state_city_{}.svg'.format(i), bbox_inches=bbox) visual.plot_state_city(rho_GHZ_list[i], color=c, alpha=a).savefig( 'state_cities/GHZ_state_city_{}.svg'.format(i), bbox_inches=bbox) visual.plot_state_city( theoretical_rho_T_list[i], color=c, alpha=a).savefig('state_cities/Theo_T_state_city_{}.svg'.format(i), bbox_inches=bbox) visual.plot_state_city(rho_T_list[i], color=c, alpha=a).savefig( 'state_cities/T_state_city_{}.svg'.format(i), bbox_inches=bbox)
import numpy as np from qiskit import * q = QuantumRegister(3, 'q') circ = QuantumCircuit(q) circ.h(q[0]) circ.cx(q[0], q[1]) circ.cx(q[0], q[2]) circ.draw() backend = BasicAer.get_backend('statevector_simulator') job = execute(circ, backend) result = job.result() outputstate = result.get_statevector(circ, decimals=3) print(outputstate) from qiskit.visualization import plot_state_city plot_state_city(outputstate)
# Add CNOT gate on control qubit 0 and target qubit 3 to create GHZ state circ.cx(0, 2) # Creating a barrier to delimninate state prepartaion from measurement circ.barrier(range(3)) circ.draw(output='mpl', filename='ghz_images/ghz_quantum_circuit.png') # Now let's plot the density matrix of our GHZ state # Note that if you are using an IDE like pycharm, then plot_state_city will not display any results # I will include in a later more complete jupyter noteboook how to view plot_state_city in an IDE # For now, my results are summarized in ghz_density_matrix quantum_job = execute(circ, backend) result = quantum_job.result() ghz_state = result.get_statevector(circ, decimals=3) plot_state_city(ghz_state) # Creating a measurement circuit to map our quantum state to a classical output meas = QuantumCircuit(3, 3) meas.measure(range(3), range(3)) meas.draw(output='mpl') # Combing quantum and measurement circuits ghz_circuit = circ + meas ghz_circuit.draw(output='mpl') # To simulate the behavior of our full GHZ circuit we will use qiskit Aer's QASM simulator
def qonduit_visualization_state_plot_state_city(state): return interactive(lambda sv: display(plot_state_city(sv)), sv=fixed(state))
circ.cx(0, 3) circ.cx(1, 3) circ.cx(2, 3) circ.h(0) circ.h(1) circ.h(2) circ.h(3) # circ.draw() backend = Aer.get_backend('statevector_simulator') job = execute(circ, backend) result = job.result() outputstate = result.get_statevector(circ, decimals=3) #print(outputstate) plt.show(plot_state_city(outputstate)) meas = QuantumCircuit(4, 4) meas.barrier(range(4)) meas.measure(range(4), range(4)) qc = circ + meas plt.show(qc.draw(output='mpl')) # print(qc) backend_sim = Aer.get_backend('qasm_simulator') job_sim = execute(qc, backend_sim, shots=1024) result_sim = job_sim.result() counts = result_sim.get_counts(qc) #print(counts)
def collisional_model(self, backend='qasm_simulator', live=False, noise_model=None, channel='amplitude_damping', shots=1024, measured_qubits=(), max_collisions=5, theta=np.pi / 2, concurrence=False, tangle=False, witness=False, tangle_witness=True, markov=True, print_results=True, save_results=False, directory=None, full=True, initial_statevector=np.array([0.5, 0.5])): self.channel = channel self.markov = markov if self.state == 'h': self.qc.h(0) elif self.state == 'GHZ_teleport': initial_state = initial_statevector initial_state /= np.linalg.norm(initial_state) self.qc.initialize(initial_state, [0]) self.theoretical_rho = np.outer(initial_state, initial_state) self.GHZ([1, 2, 3]) elif self.state == 'D': self.D() if full: self.theoretical_rho = self.full_theoretical_D(self.n, self.k) else: self.theoretical_rho = self.theoretical_D(self.n, self.k) elif self.state == 'W': self.W() if full: self.theoretical_rho = self.full_theoretical_D(self.n, self.k) else: self.theoretical_rho = self.theoretical_D(self.n, self.k) elif self.state == 'GHZ': self.GHZ(range(self.n)) if full: self.theoretical_rho = self.full_theoretical_GHZ(self.n) else: self.theoretical_rho = self.theoretical_GHZ(self.n) self.histogram_data = [] self.directory = directory if backend == 'qasm_simulator': self.backend = Aer.get_backend('qasm_simulator') self.coupling_map = None if noise_model: self.noise_model = noise_model self.basis_gates = self.noise_model.basis_gates else: self.noise_model = None self.basis_gates = None else: provider = IBMQ.get_provider(group='open') self.device = provider.get_backend(backend) self.properties = self.device.properties() self.coupling_map = self.device.configuration().coupling_map self.noise_model = noise.device.basic_device_noise_model( self.properties) self.basis_gates = self.noise_model.basis_gates if save_results: visual.plot_error_map(self.device).savefig( '{}/error_map.png'.format(self.directory)) if live: check = input( "Are you sure you want to run the circuit live? [y/n]: ") if check == 'y' or check == 'yes': self.backend = self.device else: self.backend = Aer.get_backend('qasm_simulator') else: self.backend = Aer.get_backend('qasm_simulator') self.unitary_backend = Aer.get_backend('unitary_simulator') self.shots = shots if self.state == 'GHZ_teleport': self.a = 3 self.b = 3 elif measured_qubits == (): self.a = 0 self.b = self.n - 1 else: self.a = measured_qubits[0] self.b = measured_qubits[1] self.max_collisions = max_collisions self.theta = theta if self.max_collisions > 0: if self.channel == 'phase_damping_one_qubit': self.ancilla = QuantumRegister(1, 'a') self.qc.add_register(self.ancilla) elif not markov: self.ancilla = QuantumRegister(3, 'a') self.qc.add_register(self.ancilla) self.qc.h(self.ancilla[2]) self.qc.cx(self.ancilla[2], self.ancilla[1]) self.qc.cx(self.ancilla[1], self.ancilla[0]) else: self.ancilla = QuantumRegister(self.max_collisions, 'a') self.qc.add_register(self.ancilla) if self.channel == 'amplitude_damping': self.U = self.amplitude_damping_operator(full) elif self.channel == 'phase_damping' or 'phase_damping_one_qubit': self.U = self.phase_damping_operator(full) elif self.channel == 'heisenberg': print('Not yet programmed') quit() self.U = self.heisenberg_operator() if tangle_witness: self.tangle_witness_GHZ = 3 / 4 * np.kron(np.kron( I, I), I) - self.full_theoretical_GHZ(3) self.tangle_witness_tri = 1 / 2 * np.kron(np.kron( I, I), I) - self.full_theoretical_GHZ(3) counts_list = [] rho_list = [] theoretical_rho_list = [] concurrence_list = [] theoretical_concurrence_list = [] tangle_ub_list = [] tangle_lb_list = [] theoretical_tangle_list = [] fidelity_list = [] witness_list = [] tangle_witness_GHZ_list = [] tangle_witness_tri_list = [] trace_squared_list = [] for k in range(self.max_collisions + 1): if k > 0: self.collision(k) if witness: counts = self.measure(witness) else: rho = self.tomography(full) self.evolve_theoretical_rho(full) else: if witness: counts = 1 / 2 else: rho = self.tomography(full) if save_results: visual.plot_state_city(rho).savefig( '{}/state_city_{}.png'.format(self.directory, k)) visual.plot_state_paulivec(rho).savefig( '{}/paulivec_{}.png'.format(self.directory, k)) if witness: counts_list.append(counts) else: rho_list.append(rho) theoretical_rho_list.append(self.theoretical_rho) if not full and self.state != 'GHZ_teleport': C = self.concurrence(rho) concurrence_list.append(C) TC = self.concurrence(self.theoretical_rho) theoretical_concurrence_list.append(TC) if tangle: T_ub = self.tangle_ub(rho) tangle_ub_list.append(T_ub) #T_lb = self.tangle_lb(rho) #tangle_lb_list.append(T_lb) if self.state == 'GHZ': TT = np.exp(np.log(np.cos(self.theta)) * k) theoretical_tangle_list.append(TT) F = state_fidelity(theoretical_rho_list[0], rho) fidelity_list.append(F) if witness: W = np.real( np.trace( np.matmul(self.full_theoretical_GHZ(self.n), rho))) witness_list.append(W) if tangle_witness and full: TWGHZ = np.real( np.trace(np.matmul(self.tangle_witness_GHZ, rho))) tangle_witness_GHZ_list.append(TWGHZ) TWtri = np.real( np.trace(np.matmul(self.tangle_witness_tri, rho))) tangle_witness_tri_list.append(TWtri) Tr2 = np.real(np.trace(np.matmul(rho, rho))) trace_squared_list.append(Tr2) if print_results: print("Collision Number:", k) print("Original Density Matrix:") print(theoretical_rho_list[0]) print("Theoretical Density Matrix:") print(self.theoretical_rho) print("Measured Density Matrix:") print(rho) print("Trace:", np.real(np.trace(rho))) print("Trace Squared:", Tr2) if concurrence: print("Concurrence:", C) print("Theoretical Concurrence:", TC) if tangle: print("Tangle Upper Bound:", T_ub) #print("Tangle Lower Bound:", T_lb) print("Theoretical Tangle:", TT) print("Fidelity:", F) if witness: print("Witness:", W) if tangle_witness and full: print("GHZ Tangle Witness:", TWGHZ) print("Tripartite Tangle Witness:", TWtri) print("Eigenvalues:", np.sort(np.real(np.linalg.eigvals(rho)))[::-1]) print( "\n-----------------------------------------------------------------------------------\n" ) if print_results: if save_results: visual.circuit_drawer( self.qc, filename='{}/constructed_circuit.png'.format( self.directory), output='mpl') else: print(self.qc) print("Constructed Circuit Depth: ", self.qc.depth()) try: transpiled_qc = transpile(self.qc, self.device, optimization_level=3) if save_results: visual.circuit_drawer( transpiled_qc, filename='{}/transpiled_circuit.png'.format( self.directory), output='mpl') print("Transpiled Circuit Depth: ", self.qc.depth()) except: pass print() if save_results: visual.plot_histogram( self.histogram_data, title=self.state).savefig( '{}/histogram.png'.format(self.directory)) return counts_list, theoretical_rho_list, rho_list, theoretical_concurrence_list, concurrence_list, \ theoretical_tangle_list, tangle_ub_list, tangle_lb_list, fidelity_list, witness_list, \ tangle_witness_GHZ_list, tangle_witness_tri_list, trace_squared_list
circ.h(0) circ.cx(0, 1) circ.cx(0, 2) circ.draw('mpl') from qiskit import Aer backend = Aer.get_backend('statevector_simulator') job = execute(circ, backend) result = job.result() outputstate = result.get_statevector(circ, decimals=3) print(outputstate) from qiskit.visualization import plot_state_city plot_state_city(outputstate) backend = Aer.get_backend('unitary_simulator') job = execute(circ, backend) result = job.result() print(result.get_unitary(circ, decimals=3)) meas = QuantumCircuit(3, 3) meas.barrier(range(3)) meas.measure(range(3), range(3)) qc = circ + meas qc.draw() backend_sim = Aer.get_backend('qasm_simulator') job_sim = execute(qc, backend_sim, shots=1024) result_sim = job_sim.result()
# Get basis gates from noise model basis_gates = noise_model.basis_gates # Make a circuit circ = QuantumCircuit(3, 3) circ.h(0) circ.cx(0, 1) circ.cx(1, 2) circ.measure([0, 1, 2], [0, 1, 2]) # execute the quantum circuit result = execute( circ, Aer.get_backend("statevector_simulator"), basis_gates=basis_gates, noise_model=noise_model, ).result() psi = result.get_statevector(circ) counts = result.get_counts(0) circ.draw(output="mpl") plt.show() plot_state_city(psi) plot_state_hinton(psi) plot_state_qsphere(psi) plot_state_paulivec(psi) plot_bloch_multivector(psi) plot_histogram(counts, color="c")
outcome.append(0) if 11 in observation: outcome.append(1) if 10 in observation: outcome.append(0) circuit.measure([2], [2]) circuit.barrier() print("Quantum circuit:\n{}".format(circuit)) print("[qubit1, qubit0] = {}".format(outcome)) backend = Aer.get_backend('statevector_simulator') result = execute(circuit, backend=backend).result() statevector = result.get_statevector(circuit) print("State vector: {}".format(statevector)) fig = plot_state_city(statevector) fig.savefig( "/home/agustinsilva447/Github/Reinforcement-Learning/Prueba 1/state.png" ) backend = Aer.get_backend('qasm_simulator') result = execute(circuit, backend=backend, shots=1000).result() counts = result.get_counts(circuit) print("Counts: {}".format(counts)) fig = plot_histogram(counts) fig.savefig( "/home/agustinsilva447/Github/Reinforcement-Learning/Prueba 1/counts.png" ) for out in counts.keys(): if (int(out[2]) == outcome[1]) and (int(out[1]) == outcome[0]):