Computer Quantistico - Backend/Simulator
Statevector simulator
!pip install qiskit
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
from qiskit import Aer, execute
from qiskit.tools.visualization import plot_state_city
from qiskit.providers.aer import StatevectorSimulator
import qiskit.tools.visualization
from qiskit.tools.visualization import plot_state_qsphere
qubits = QuantumRegister(2, 'qubits')
circuit = QuantumCircuit(qubits)
circuit.h(qubits[0])
circuit.cx(qubits[0], qubits[1])
# Statevector Simulator Backend
simulator = Aer.get_backend('statevector_simulator')

result = execute(circuit, simulator).result()
statevector = result.get_statevector(circuit)
plot_state_city(statevector, title='Bell state StateVector',alpha=0.2,color=['green','green'])

plot_state_qsphere(statevector,figsize=(8,7))



from qiskit.tools.visualization import plot_state_paulivec
plot_state_paulivec(statevector)

qr = QuantumRegister(2, 'qr')
cr = ClassicalRegister(2, 'cr')
circ = QuantumCircuit(qr, cr)
circ.h(qr[0])
circ.cx(qr[0], qr[1])
circ.measure(qr, cr)

simulator = Aer.get_backend('statevector_simulator')
result = execute(circ, simulator,shots=1024).result()
statevector = result.get_statevector(circ)
plot_state_city(statevector, title='Bell state dopo Meusurement',alpha=0.4, color=['red','blue'])






UnitarySimulator

Con Qiskit Aer possiamo testare la unitary matrix di un circuito quantistico.

UnitarySimulator
Lo UnitarySimulator costruisce la unitary matrix per un Qiskit QuantumCircuit applicando ogni gate alla identity matrix. Il circuito può contenere solo gates se inseriamo reset o measure si ha errore.


import numpy as np

from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
from qiskit import Aer, execute
from qiskit.providers.aer import UnitarySimulator
qr = QuantumRegister(2)
circ = QuantumCircuit(qr)
circ.h(qr[0])
circ.x(qr[1])
# Select UnitarySimulator
simulator = Aer.get_backend('unitary_simulator')
result = execute(circ, simulator).result()
unitary = result.get_unitary(circ)
print("Circuito unitary simulator:\n", unitary)

Circuito unitary simulator:
[[ 0. +0.00000000e+00j 0. +0.00000000e+00j
0.70710678+0.00000000e+00j 0.70710678-8.65956056e-17j]
[ 0. +0.00000000e+00j 0. +0.00000000e+00j
0.70710678+0.00000000e+00j -0.70710678+8.65956056e-17j]
[ 0.70710678+0.00000000e+00j 0.70710678-8.65956056e-17j
0. +0.00000000e+00j 0. +0.00000000e+00j]
[ 0.70710678+0.00000000e+00j -0.70710678+8.65956056e-17j
0. +0.00000000e+00j 0. +0.00000000e+00j]]

Possiamo anche inserire un initial state per l'UnitarySimulator questa è l'initial unitary matrix ed il risultato si ottiene applicando il circuit unitary all'initial unitary matrix.


qr = QuantumRegister(2)
circ = QuantumCircuit(qr)
circ.iden(qr)

# Set initial unitary
opts = {"initial_unitary": np.array([[ 1, 1, 0, 0],
[ 0, 0, 1, -1],
[ 0, 0, 1, 1],
[ 1, -1, 0, 0]] / np.sqrt(2))}


simulator = Aer.get_backend('unitary_simulator')

result = execute(circ, simulator, backend_options=opts).result()
unitary = result.get_unitary(circ)

print("Initial Unitary:\n", unitary)

Initial Unitary:
[[ 0.70710678+0.j 0.70710678+0.j 0. +0.j 0. +0.j]
[ 0. +0.j 0. +0.j 0.70710678+0.j -0.70710678+0.j]
[ 0. +0.j 0. +0.j 0.70710678+0.j 0.70710678+0.j]
[ 0.70710678+0.j -0.70710678+0.j 0. +0.j 0. +0.j]]




Esempi di alcuni Backend per effettuare test su IBMQ


from qiskit import QuantumCircuit, execute, IBMQ
from qiskit.visualization import plot_gate_map
%matplotlib inline
IBMQ.save_account(token)
#provider = IBMQ.load_account()
Provider = IBMQ.get_provider(hub='ibm-q')
backend = Provider.get_backend('ibmq_vigo')
plot_gate_map(backend)

Backend Vigo


backend = Provider.get_backend('ibmq_16_melbourne')
plot_gate_map(backend)

Backend melbourne



backend = Provider.get_backend('ibmq_rome')
plot_gate_map(backend)

Backend Rome