backend_device = ibmqprovider.get_backend('ibmqx4')

# 0. build circuit
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
q = QuantumRegister(2)
c = ClassicalRegister(2)
circ = QuantumCircuit(q, c)
circ.cx(q[0], q[1])
circ.cx(q[0], q[1])
circ.cx(q[0], q[1])
circ.cx(q[0], q[1])
circ.measure(q, c)

# draw circuit
from qiskit.tools.visualization import plot_circuit
plot_circuit(circ)

# 1. standard compile -- standard qiskit passes, when no PassManager given
from qiskit import transpiler, load_qasm_string
qobj_standard = transpiler.compile(circ, backend_device)
compiled_standard = load_qasm_string(
    qobj_standard['circuits'][0]['compiled_circuit_qasm'])
plot_circuit(compiled_standard)

# 2. custom compile -- customize PassManager to run specific circuit transformations
from qiskit.transpiler.passes import CXCancellation
pm = transpiler.PassManager()
pm.add_pass(CXCancellation())
qobj_custom = transpiler.compile(circ, backend_device, pass_manager=pm)
compiled_custom = load_qasm_string(
    qobj_custom['circuits'][0]['compiled_circuit_qasm'])
Ejemplo n.º 2
0
    0, 0, 0, 0, 1 / math.sqrt(8) * complex(1, 0),
    1 / math.sqrt(8) * complex(0, 1), 0, 0, 0, 0,
    1 / math.sqrt(4) * complex(1, 0), 1 / math.sqrt(8) * complex(1, 0)
]

circuit.initialize(desired_vector, [qr[0], qr[1], qr[2], qr[3]])

circuit.measure(qr[0], cr[0])
circuit.measure(qr[1], cr[1])
circuit.measure(qr[2], cr[2])
circuit.measure(qr[3], cr[3])

QASM_source = circuit.qasm()

print(QASM_source)
plot_circuit(circuit)

###############################################################
# Execute on a simulator backend.
###############################################################
shots = 1024

# Desired vector
print("Desired probabilities...")
print(str(list(map(lambda x: format(abs(x * x), '.3f'), desired_vector))))

# Initialize on local simulator
result = execute(circuit, backend='local_qasm_simulator', shots=shots).result()

print("Probabilities from simulator...[%s]" % result)
n_qubits_qureg = qr.size
Ejemplo n.º 3
0
from qiskit import QuantumProgram
from qiskit.tools.visualization import plot_histogram, circuit_drawer, plot_circuit

qp = QuantumProgram()

nq = 2  # number of qubits
q = qp.create_quantum_register("q", nq)
c = qp.create_classical_register("c", nq)

circuits = ['testQ']
testQ = qp.create_circuit(circuits[0], [q], [c])

testQ.h(q[0])
testQ.cx(q[0], q[1])

testQ.measure(q[0], c[0])
testQ.measure(q[1], c[1])

results = qp.execute(circuits,
                     backend='local_qasm_simulator',
                     shots=1024,
                     seed=1)

plot_histogram(results.get_counts(circuits[0]))
#circuit_drawer(testQ)
plot_circuit(testQ)
Ejemplo n.º 4
0
backend = lowest_pending_jobs()
print("The best backend is " + backend)

q = QuantumRegister(2)
c = ClassicalRegister(2)
qc = QuantumCircuit(q, c)
qc.h(q[0])
qc.cx(q[0], q[1])
qc.measure(q, c)
job_exp = execute(qc, backend=backend, shots=1024, max_credits=3)

lapse = 0
interval = 10
while not job_exp.done:
    print('Status @ {} seconds'.format(interval * lapse))
    print(job_exp.status)
    time.sleep(interval)
    lapse += 1
print(job_exp.status)

plot_histogram(job_exp.result().get_counts(qc))

print('You have made entanglement!')

# Keita Memo
# I don't know why but circuit_drawer does not work correctly.
# So I use plot_circuit instead.
# circuit_drawer(qc)
plot_circuit(qc)
Ejemplo n.º 5
0
    0,
    0,
    1 / math.sqrt(4) * complex(1, 0),
    1 / math.sqrt(8) * complex(1, 0)]

circuit.initialize(desired_vector, [qr[0], qr[1], qr[2], qr[3]])

circuit.measure(qr[0], cr[0])
circuit.measure(qr[1], cr[1])
circuit.measure(qr[2], cr[2])
circuit.measure(qr[3], cr[3])

QASM_source = Q_program.get_qasm("initializer_circ")

print(QASM_source)
plot_circuit(circuit)

###############################################################
# Set the backend name and coupling map.
###############################################################
device = 'ibmqx2'
coupling_map = {0: [1, 2],
                1: [2],
                2: [],
                3: [2, 4],
                4: [2]}
circuits = ['initializer_circ']
shots = 1024

###############################################################
# Set up the API and execute the program.