Creating a Bell state

 1import qblaze
 2
 3sim = qblaze.Simulator()
 4
 5sim.h(0)     # create superposition: |00⟩ ↦ (|00⟩+|10⟩)/√2
 6sim.cx(0, 1) # entangle the two qubits: (|00⟩+|11⟩)/√2
 7
 8sim.dump()   # show state vector on stderr
 9
10# measure both qubits:
11c0 = sim.measure(0)
12c1 = sim.measure(1)
13
14assert c0 == c1 # measurement outcomes are perfectly correlated
15
16# reset qubits (just for demonstration):
17if c0:
18    sim.x(0)
19if c1:
20    sim.x(1)
 1import qiskit
 2
 3# Create a 2-qubit quantum circuit
 4q = qiskit.QuantumRegister(2)
 5r = qiskit.ClassicalRegister(2)
 6circ = qiskit.QuantumCircuit(q, r)
 7
 8circ.h(q[0])        # create superposition: |00⟩ ↦ (|00⟩+|10⟩)/√2
 9circ.cx(q[0], q[1]) # entangle the two qubits: (|00⟩+|11⟩)/√2
10circ.measure(q, r)  # measure both qubits
11
12import qblaze.qiskit
13
14# run 128 random simulations
15backend = qblaze.qiskit.Backend()
16shots = 128
17result = backend.run(circ, shots=shots).result()
18counts = result.data()['counts']
19assert sorted(counts.keys()) == ['0x0', '0x3']
20
21print(counts)
 1#define _GNU_SOURCE
 2#define _USE_MATH_DEFINES
 3#include <qblaze.h>
 4#include <assert.h>
 5#include <math.h>
 6#include <stdio.h>
 7#include <unistd.h>
 8
 9int main() {
10    uint64_t random64[2];
11    int r = getentropy(random64, sizeof(random64));
12    if (r < 0) return 1;
13
14    QBlazeSimulator *sim = qblaze_new(NULL);
15    if (!sim) return 1;
16
17    // create superposition: |00⟩ ↦ (|00⟩+|10⟩)/√2
18    r = qblaze_apply_u3(sim, 0, M_PI_2, 0, M_PI); // H 0
19    if (r < 0) goto fail;
20
21    // entangle the two qubits: (|00⟩+|11⟩)/√2
22    r = qblaze_apply_mcx(sim, (struct QBlazeControl[]){{0, true}}, 1, 1); // CX 0, 1
23    if (r < 0) goto fail;
24
25    // measure qubit 0
26    r = qblaze_measure(sim, 0, random64[0], NULL, NULL);
27    if (r < 0) goto fail;
28    bool c0 = r;
29    printf("Measured qubit 0 as %d\n", r);
30
31    // measure qubit 1
32    r = qblaze_measure(sim, 1, random64[1], NULL, NULL);
33    if (r < 0) goto fail;
34    bool c1 = r;
35    printf("Measured qubit 1 as %d\n", r);
36
37    assert(c0 == c1); // measurement outcomes are perfectly correlated
38
39    qblaze_del(sim);
40    return 0;
41
42fail:
43    fprintf(stderr, "Error %d\n", r);
44    qblaze_del(sim);
45    return 1;
46}