def create_images(gate, theta=0.0, phi=0.0, lam=0.0): # Set the loop parameters steps = 20.0 theta_steps = theta / steps phi_steps = phi / steps lam_steps = lam / steps n, theta, phi, lam = 0, 0.0, 0.0, 0.0 # Create image and animation tools global q_images, b_images, q_filename, b_filename b_images = [] q_images = [] b_filename = "animated_qubit" q_filename = "animated_qsphere" # The image creation loop while n < steps + 1: qc = QuantumCircuit(1) if gate == "u3": qc.u3(theta, phi, lam, 0) title = "U3: \u03B8 = " + str(round( theta, 2)) + " \u03D5 = " + str(round( phi, 2)) + " \u03BB = " + str(round(lam, 2)) elif gate == "u2": qc.u2(phi, lam, 0) title = "U2: \u03D5 = " + str(round(phi, 2)) + " \u03BB = " + str( round(lam, 2)) else: qc.h(0) qc.u1(phi, 0) title = "U1: \u03D5 = " + str(round(phi, 2)) # Get the statevector of the qubit # Create Bloch sphere images plot_bloch_multivector(get_psi(qc), title).savefig('images/bloch' + str(n) + '.png') imb = Image.open('images/bloch' + str(n) + '.png') b_images.append(imb) # Create Q sphere images plot_state_qsphere(psi).savefig('images/qsphere' + str(n) + '.png') imq = Image.open('images/qsphere' + str(n) + '.png') q_images.append(imq) # Rev our loop n += 1 theta += theta_steps phi += phi_steps lam += lam_steps
def draw_quantum_circuit(qc: QuantumCircuit, draw_circuit=True, draw_unitary=True, draw_final_state=True, draw_bloch_sphere=False, draw_q_sphere=False, draw_histogram=False): if draw_circuit: # Visualize the quantum circuit print('Quantum circuit:') print(qc.draw()) if draw_unitary: try: # Visualize the unitary operator backend = Aer.get_backend('unitary_simulator') unitary = execute(qc, backend).result().get_unitary() print('Unitary:') for row in unitary: print(' '.join([display_complex(elem) for elem in row])) except QiskitError: # If a qunatum circuit contains a measure operation, the process is # not reversible anymore, and hence cannot be represented by a # Unitary matrix. We just ignore this operation in that case. pass if draw_final_state or draw_bloch_sphere or draw_q_sphere: # Visualize the final state # final_state for 2 qubits = a |00> + b |01> + c |10> + d |11> backend = Aer.get_backend('statevector_simulator') final_state = execute(qc, backend).result().get_statevector() if draw_final_state: print('Final state:') for elem in final_state: print(display_complex(elem)) if draw_bloch_sphere: plot_bloch_multivector(final_state).show() if draw_q_sphere: plot_state_qsphere(final_state).show() if draw_histogram: backend = Aer.get_backend('statevector_simulator') results = execute(qc, backend).result().get_counts() plot_histogram(results).show()
def run(): #create circuitmanager for handling grover's alg c = GroverCircuit(4, bonus_q=5) c.initHGates() #add h-gates to top 4 lines c.circuit.initialize([1 / np.sqrt(2), -1 / np.sqrt(2)], 8) #construct oracle c.circuit.barrier() c.circuit.append(sudokuOracle(), [0, 1, 2, 3, 4, 5, 6, 7]) c.circuit.mct([4, 5, 6, 7], 8) c.circuit.append(sudokuOracle(), [0, 1, 2, 3, 4, 5, 6, 7]) #add grover c.circuit.append(diffuser(4), [0, 1, 2, 3]) #do it again #construct oracle c.circuit.barrier() c.circuit.append(sudokuOracle(), [0, 1, 2, 3, 4, 5, 6, 7]) c.circuit.mct([4, 5, 6, 7], 8) c.circuit.append(sudokuOracle(), [0, 1, 2, 3, 4, 5, 6, 7]) #add grover c.circuit.append(diffuser(4), [0, 1, 2, 3]) c.circuit.barrier() state = Statevector.from_instruction(c.circuit) print(state) plot_state_qsphere(state) measureInRange(c.cm, 0, 4) c.cm.simulate() print(c.cm.counts) print('\n') c.cm.printTable() print('\n') c.cm.printQubitStates() print('\n') c.cm.printEntanglementTable() c.cm.printEntanglements() print(c.circuit.draw()) c.draw()
def not_corrected(error, ry_error, rz_error): # Non-corrected code qco = QuantumCircuit(1, 1) print("\nOriginal qubit, in state |0>") display(plot_bloch_multivector(get_psi(qco))) display(plot_state_qsphere(get_psi(qco))) # Add error add_error(error, qco, ry_error, rz_error) print("\nQubit with error...") display(plot_bloch_multivector(get_psi(qco))) display(plot_state_qsphere(get_psi(qco))) qco.measure(0, 0) display(qco.draw('mpl')) job = execute(qco, backend, shots=1000) counts = job.result().get_counts() print("\nResult of qubit error:") print("-----------------------") print(counts)
def qgate_out(circuit, start): # Print the circuit psi = get_psi(circuit) if start != "n": print("\nCircuit:") print("--------") print(circuit) print("\nState vector:") print("-------------") print(np.around(psi, decimals=3)) display(plot_bloch_multivector(psi)) if circuit.num_qubits > 1 and gate in control_gates: display(plot_state_qsphere(psi)) return (psi)
def s_vec(circuit): backend = Aer.get_backend('statevector_simulator') print(circuit.num_qubits, "qubit quantum circuit:\n------------------------") print(circuit) psi = execute(circuit, backend).result().get_statevector(circuit) print("State vector for the", circuit.num_qubits, "qubit circuit:\n\n", psi) print("\nState vector as Bloch sphere:") display(plot_bloch_multivector(psi)) print("\nState vector as Q sphere:") display(plot_state_qsphere(psi)) measure(circuit) input("Press enter to continue...\n")
def get_psi(circuit, vis): from qiskit.visualization import plot_bloch_multivector, plot_state_qsphere from qiskit import Aer, execute global psi backend = Aer.get_backend('statevector_simulator') psi = execute(circuit, backend).result().get_statevector(circuit) if vis == "Q": display(plot_state_qsphere(psi)) elif vis == "M": print(psi) elif vis == "B": display(plot_bloch_multivector(psi)) vis = "" print_psi(psi) return (psi)
import qiskit.quantum_info as qi q_sim = Aer.get_backend('qasm_simulator') s_sim = Aer.get_backend('statevector_simulator') u_sim = Aer.get_backend('unitary_simulator') #%% qc = QuantumCircuit(2) qc.h(0) qc.cx(0, 1) qc.draw() #%% job = execute(qc, s_sim) vector = job.result().get_statevector() plot_state_qsphere(vector) #%% qc = QuantumCircuit(2) qc.h(0) qc.cx(0, 1) qc.draw() #%% sv = qi.Statevector.from_instruction(qc) plot_state_qsphere(sv) #%% sv2 = qi.Statevector.from_label('11') sv2 = sv2.evolve(qc) plot_state_qsphere(sv2)
return myqc # creat random parameters to build a random state on the Bloch sphere theta = random.uniform(0,1)*2*pi phi = random.uniform(0,1)*2*pi lam = random.uniform(0,1)*2*pi print("parameters theta, phi, and lam are: ", theta, phi, lam) random_circuit = newqc(theta, phi, lam, 0) state = Statevector.from_instruction(random_circuit) sv = state.data print("The state vector of our random state on the Bloch sphere is: " , sv) plot_bloch_multivector (state) plot_state_qsphere(state, show_state_labels=True) ############################################################################################################################# # ## Part 2) # defining the function to measure the similarity of 2 given quantum circuits using thr swap test def similarity(circuit1, circuit2, num_qubits): overlap_test_qc = QuantumCircuit(3,1) overlap_test_qc.h(0) overlap_test_qc.compose(circuit1, qubits=[1], inplace=True) overlap_test_qc.compose(circuit2, qubits=[2], inplace=True) for i in range(num_qubits):
def update_output(): out_state = execute(qc,backend).result().get_statevector() if qsphere: image.value = plot_state_qsphere(out_state) else: image.value = plot_bloch_multivector(out_state)
def qonduit_visualization_state_plot_state_qsphere(state): return interactive(lambda sv: display(plot_state_qsphere(sv)), sv=fixed(state))
plot_histogram([counts, second_counts], legend=legend) plot_histogram([counts, second_counts], legend=legend, sort='desc', figsize=(15, 12), color=['orange', 'black'], bar_labels=False) from qiskit.visualization import plot_state_city, plot_bloch_multivector from qiskit.visualization import plot_state_paulivec, plot_state_hinton from qiskit.visualization import plot_state_qsphere backend = BasicAer.get_backend('statevector_simulator') result = execute(bell, backend).result() psi = result.get_statevector(bell) plot_state_city(psi) plot_state_hinton(psi) plot_state_qsphere(psi) plot_state_paulivec(psi) plot_bloch_multivector(psi) plot_state_city(psi, title="My City", color=['black', 'orange']) plot_state_hinton(psi, title="My Hinton")
def shor_corrected(error, ry_error, rz_error): # A combination of a three qubit phase flip code, and 3 bit flip codes qc = QuantumCircuit(9, 1) print("\nOriginal LSB qubit, in state |...0>") display(plot_state_qsphere(get_psi(qc))) # Start of phase flip code qc.cx(0, 3) qc.cx(0, 6) qc.h(0) qc.h(3) qc.h(6) qc.barrier([x for x in range(qc.num_qubits)]) # Start of bit flip codes qc.cx(0, 1) qc.cx(3, 4) qc.cx(6, 7) qc.cx(0, 2) qc.cx(3, 5) qc.cx(6, 8) # Error code add_error(error, qc, ry_error, rz_error) print( "Qubit with error... LSB can be in |...0> and in |...1>, with various phase." ) display(plot_state_qsphere(get_psi(qc))) display(qc.draw('mpl')) # End of bit flip codes qc.cx(0, 1) qc.cx(3, 4) qc.cx(6, 7) qc.cx(0, 2) qc.cx(3, 5) qc.cx(6, 8) qc.ccx(1, 2, 0) qc.ccx(4, 5, 3) qc.ccx(8, 7, 6) # End of phase flip code qc.h(0) qc.h(3) qc.h(6) qc.cx(0, 3) qc.cx(0, 6) qc.ccx(6, 3, 0) qc.barrier([x for x in range(qc.num_qubits)]) qc.measure(0, 0) print("Error corrected qubit... LSB in |...0> with phase 0.") display(plot_state_qsphere(get_psi(qc))) display(qc.draw('mpl')) job = execute(qc, backend, shots=1000) counts = job.result().get_counts() print("\nResult of qubit error after Shor code correction:") print("--------------------------------------------------") print(counts)
c.cx(0,2) c.cx(1,2) c.mct([2],3) c.cx(0,2) c.cx(1,2) c.barrier() #diffuser c.h(0) c.h(1) c.x(0) c.x(1) c.barrier(0) c.h(1) c.mct([0],1) c.h(1) c.barrier(0) c.x(0) c.x(1) c.h(0) c.h(1) c.draw(output="mpl") pyplot.show() state = Statevector.from_instruction(cm.circuit) plot_state_qsphere(state) pyplot.show()