def city(statevector): dark_style(False) figure = plot_state_city(statevector) figure.savefig(temppath('density_matrix_cityscape_light.png')) dark_style(True) figure = plot_state_city(statevector) figure.savefig(temppath('density_matrix_cityscape_dark.png'))
def process_results(qc, result): if result.backend_name in 'statevector_simulator': statevector = result.get_statevector(qc) logger.info("State vector is\n{0}".format(statevector)) logger.debug("Plotting") from qiskit.tools.visualization import plot_state_city plot_state_city(statevector) elif result.backend_name in 'unitary_simulator': unitary = result.get_unitary(qc) logger.info("Circuit unitary is:\n{0}".format(unitary)) else: # For qasm and real devices counts = result.get_counts(qc) logger.info("{0} results: \n {1}".format(len(counts), counts)) logger.info("Time taken {}".format(result.time_taken)) max_val = max(counts.values()) max_val_status = max(counts, key=lambda key: counts[key]) logger.info( "Max value is {0} ({2:4.2f} accuracy) for status {1}".format( max_val, max_val_status, max_val / 8192)) logger.debug("Plotting") from qiskit.tools.visualization import plot_histogram plot_histogram(counts)
circ.draw(output='mpl') # Import Aer from qiskit import BasicAer # Run the quantum circuit on a statevector simulator backend backend = BasicAer.get_backend('statevector_simulator') # Create a Quantum Program for execution job = execute(circ, backend) result = job.result() outputstate = result.get_statevector(circ, decimals=3) print(outputstate) from qiskit.tools.visualization import plot_state_city plot_state_city(outputstate) # Run the quantum circuit on a unitary simulator backend backend = BasicAer.get_backend('unitary_simulator') job = execute(circ, backend) result = job.result() # Show the results print(result.get_unitary(circ, decimals=3))
# Add a CX (CNOT) gate on control qubit 0 and target qubit 1 circuit.cx(0, 1) circuit.t(0) #circuit.measure_all() # Execute the circuit on the statevector simulator job = execute(circuit, simulator) # Grab results from the job result = job.result() statevector = result.get_statevector(circuit) plot_bloch_multivector(statevector, title='Bloch sphere') plot_state_city(statevector, title='State vector') print(statevector) # Returns counts counts = result.get_counts(circuit) print("\nCounts:",counts) # Draw the circuit circ_plt = circuit.draw(output='mpl') plt.show(block=False) print(circuit.draw(output='text')) # Plot a histogram fig = plot_histogram(counts) plt.show(block=True)
circuit.cx(3, 5) circuit.barrier() # Perform Bell state measurement circuit.cx(1, 3) circuit.cx(0, 2) circuit.h(1) circuit.h(0) circuit.measure([0, 1, 2, 3], [0, 1, 2, 3]) # Operate on Bob's qubit given result circuit.cx(3, 4) circuit.cz(0, 4) circuit.cx(2, 5) circuit.cz(1, 5) circuit.barrier() # Do the reverse things on Bob's qubit apply_secret_unitary(secret_0, 4, circuit, True) apply_secret_unitary(secret_1, 5, circuit, True) # Measure Bob's qubit circuit.measure([4, 5], [4, 5]) print(circuit) # Simulate circuit simulator = Aer.get_backend('statevector_simulator') result = execute(circuit, backend=simulator).result() statevector = result.get_statevector(circuit) plot_state_city(statevector)
q = QuantumRegister(3, 'q') circ = QuantumCircuit(q) #Add a H gate (Hammard gate) in qbit 0, #putting this qbit in superposition circ.h(q[0]) #Add CX (CNOT) gate on control of qbit 0 and target qbit 1, putting #the qbits in a Bell state circ.cx(q[0], q[1]) #Add CX (CNOT) gate on control of qbit 0 and target qbit 1, putting #the qbits in a GHZ state circ.cx(q[0], q[2]) circ.draw(output='mpl') backend = BasicAer.get_backend('statevector_simulator') job = execute(circ, backend) result = job.result() outputstate = result.get_statevector(circ, decimals=3) print(plot_state_city(outputstate))