Exemple #1
0
def test_qasm_nocache():
    correct_qasm = qasm_prologue(1) + "\nx q[0];\ny q[0];\nz q[0];"
    c = Circuit().x[0].y[0].z[0]
    c.run()
    c.to_qasm()
    qasm = c.to_qasm()
    assert qasm == correct_qasm
Exemple #2
0
def test_cache_then_append():
    c = Circuit()
    c.x[0]
    c.run()
    c.h[0]
    c.run()
    assert is_vec_same(c.run(), Circuit().x[0].h[0].run())
Exemple #3
0
def test_cache_then_append(backend):
    c = Circuit()
    c.x[0]
    c.run()
    c.h[0]
    c.run()
    assert np.allclose(c.run(backend=backend),
                       Circuit().x[0].h[0].run(backend=backend))
Exemple #4
0
def test_copy_empty(backend):
    c = Circuit()
    c.run(backend=backend)
    cc = c.copy(copy_backends=True)
    assert c.ops == cc.ops and c.ops is not cc.ops
    if backend in ['numpy', 'numba']:
        assert c._backends[backend].cache is None and cc._backends[backend].cache is None
        assert c._backends[backend].cache_idx == cc._backends[backend].cache_idx == -1
Exemple #5
0
def test_concat_circuit2():
    c1 = Circuit()
    c1.h[1]
    c1.run()
    c2 = Circuit()
    c2.h[0]
    c2.run()
    c1 += c2
    assert is_vec_same(c1.run(), Circuit().h[1].h[0].run())
Exemple #6
0
def test_concat_circuit1(backend):
    c1 = Circuit()
    c1.h[0]
    c1.run()
    c2 = Circuit()
    c2.h[1]
    c2.run()
    c1 += c2
    assert is_vec_same(c1.run(backend=backend), Circuit().h[0].h[1].run(backend=backend))
Exemple #7
0
def test_controlled_gate_phase_cphase(backend):
    theta = 1.2
    val = 1.0
    c0 = Circuit().cphase(theta)[0, 1]
    c1 = Circuit().x[0] + c0
    v0 = c0.run(backend=backend)
    v1 = c1.run(backend=backend)
    assert abs(abs(v0[0]) - 1) < 1e-8
    assert abs(v1[1] / v0[0] - val) < 1e-8
def test_copy_empty_numba():
    c = Circuit()
    c.run(backend='numba')
    # copy_history: deprecated.
    cc = c.copy(copy_backends=True)
    assert c.ops == cc.ops and c.ops is not cc.ops
    assert c._backends['numba'].cache is None and cc._backends[
        'numba'].cache is None
    assert c._backends['numba'].cache_idx == cc._backends[
        'numba'].cache_idx == -1
Exemple #9
0
def test_concat_circuit4():
    c1 = Circuit()
    c1.x[0]
    c2 = Circuit()
    c2.h[0]
    c = c1 + c2
    c.run()
    assert is_vec_same(c.run(), Circuit().x[0].h[0].run())
    assert is_vec_same(c1.run(), Circuit().x[0].run())
    assert is_vec_same(c2.run(), Circuit().h[0].run())
Exemple #10
0
def test_concat_circuit2(backend):
    c1 = Circuit()
    c1.h[1]
    c1.run()
    c2 = Circuit()
    c2.h[0]
    c2.run()
    c1 += c2
    assert np.allclose(c1.run(backend=backend),
                       Circuit().h[1].h[0].run(backend=backend))
Exemple #11
0
def test_swap(backend):
    p = random.random() * math.pi
    q = random.random() * math.pi
    r = random.random() * math.pi
    s = random.random() * math.pi

    c1 = Circuit().ry(p)[0].rz(q)[0].ry(r)[1].rz(s)[1]
    c2 = Circuit().ry(p)[1].rz(q)[1].ry(r)[0].rz(s)[0].swap[0, 1]

    assert np.allclose(c1.run(backend=backend), c2.run(backend=backend))
Exemple #12
0
def test_controlled_gate_phase_cu(backend):
    theta = 1.2
    phi = 1.6
    lambd = 2.3
    c0 = Circuit().cu(theta, phi, lambd)[0, 1]
    c1 = Circuit().x[0] + c0
    v0 = c0.run(backend=backend)
    v1 = c1.run(backend=backend)
    assert abs(abs(v0[0]) - 1) < 1e-8
    assert abs(v1[1] / v0[0] - math.cos(theta / 2)) < 1e-8
Exemple #13
0
def test_initial_vec(backend):
    if backend == 'qgate':
        import qgate
        try:
            qgate.__version__
        except AttributeError:
            pytest.xfail("This version of qgate doesn't support initial vec.")

    c = Circuit().h[0]
    v1 = c.run(backend=backend)
    assert np.allclose(c.run(backend=backend, initial=v1),
                       Circuit(1).run(backend=backend))
Exemple #14
0
def test_controlled_gate_phase_cu_with_gamma(backend):
    theta = 1.2
    phi = 1.6
    lambd = 2.3
    gamma = 1.4

    c0 = Circuit().cu(theta, phi, lambd, gamma)[0, 1]
    c1 = Circuit().x[0] + c0
    v0 = c0.run(backend=backend)
    v1 = c1.run(backend=backend)
    assert abs(abs(v0[0]) - 1) < 1e-8
    assert abs(v1[1] / v0[0] -
               cmath.exp(1j * gamma) * math.cos(theta / 2)) < 1e-8
Exemple #15
0
def test_concat_circuit4(backend):
    c1 = Circuit()
    c1.x[0]
    c2 = Circuit()
    c2.h[0]
    c = c1 + c2
    c.run()
    assert np.allclose(c.run(backend=backend),
                       Circuit().x[0].h[0].run(backend=backend))
    assert np.allclose(c1.run(backend=backend),
                       Circuit().x[0].run(backend=backend))
    assert np.allclose(c2.run(backend=backend),
                       Circuit().h[0].run(backend=backend))
Exemple #16
0
def sample_blueqat_3():
    c = Circuit().x[0, 1].h[0].cphase(math.pi / 2)[1, 0].cphase(math.pi /
                                                                4)[2, 0]  # 0番目
    c.h[1].cphase(math.pi / 2)[2, 1]  # 1番目
    c.h[2]  # 2番目
    r = c.run()
    print(r)
Exemple #17
0
def qft_with_3qbit_110():
    # |j0j1j2> = |000>
    c = Circuit(3)

    # |j0j1j2> = |110>
    c = c.x[0, 1]

    # j0ビットにアダマールゲートをかける。
    c = c.h[0]

    # j1ビットを制御ビットにしてj0ビットにR1をかける。
    c = c.cphase(math.pi / 2)[1, 0]

    # j2ビットを制御ビットにしてj0ビットにR2をかける。
    c = c.cphase(math.pi / 4)[2, 0]

    # j2ビットを制御ビットにしてj1ビットにR1をかける。
    c.h[1].cphase(math.pi / 2)[2, 1]

    # j2ビットにアダマールゲートをかける。
    c.h[2]

    r = c.run()
    r = list(2 * math.sqrt(2) * r)
    print("> 3qbit")
    for i, v in enumerate(r):
        b = "{:03b}".format(i)
        print("[{}]{}".format(b, v))
Exemple #18
0
def test_measurement_entangled_state(backend):
    # 1/sqrt(2) (|0> + |1>)
    c = Circuit().h[0].cx[0, 1]
    for _ in range(100):
        cnt = c.run(backend=backend, shots=1)
        result = cnt.most_common()
        assert result == [("00", 1)] or result == [("11", 1)]
Exemple #19
0
def test_measurement_after_qubits1():
    for _ in range(50):
        c = Circuit().h[0].m[0]
        a, cnt = c.run(shots=1, returns="statevector_and_shots")
        if cnt.most_common(1)[0] == ('0', 1):
            assert is_vec_same(a, np.array([1, 0]))
        else:
            assert is_vec_same(a, np.array([0, 1]))
Exemple #20
0
def test_globalphase_u_with_gamma(backend):
    theta = 1.2
    phi = 1.6
    lambd = 2.3
    gamma = -1.4
    c = Circuit().u(theta, phi, lambd, gamma)[0]
    assert abs(
        c.run(backend=backend)[0] -
        cmath.exp(1j * gamma) * math.cos(theta / 2)) < 1e-8
Exemple #21
0
def test_toffoli_gate(bits, backend):
    c = Circuit()
    if bits[0]:
        c.x[0]
    if bits[1]:
        c.x[1]
    c.ccx[0, 1, 2].m[2]
    expected_meas = "001" if bits[0] and bits[1] else "000"
    assert c.run(backend=backend, shots=1) == Counter([expected_meas])
Exemple #22
0
def test_json_dump_load():
    """A Circuit and deserialize(serialize())ed circuit returns same result.
    (However, it doesn't means they're same Circuit.)"""
    c = Circuit().h[:3].x[4].u(1.2, 3.4, 2.3, 1.0)[0].h[:].u(1.2, 3.4, 2.3)[1]
    d = serialize(c)
    j = json.dumps(d)
    d2 = json.loads(j)
    c2 = deserialize(d2)
    np.allclose(c.run(), c2.run())
Exemple #23
0
def test_controlled_gate_phase(backend):
    theta = 1.2
    phi = 1.6
    lambd = 2.3

    val = np.exp(-0.5j * theta)
    c0 = Circuit().crz(theta)[0, 1]
    c1 = Circuit().x[0] + c0
    v0 = c0.run(backend=backend)
    v1 = c1.run(backend=backend)
    assert abs(abs(v0[0]) - 1) < 1e-8
    assert abs(v1[1] / v0[0] - val) < 1e-8

    val = 1.0
    c0 = Circuit().cphase(theta)[0, 1]
    c1 = Circuit().x[0] + c0
    v0 = c0.run(backend=backend)
    v1 = c1.run(backend=backend)
    assert abs(abs(v0[0]) - 1) < 1e-8
    assert abs(v1[1] / v0[0] - val) < 1e-8

    val = 1.0
    c0 = Circuit().cu1(theta)[0, 1]
    c1 = Circuit().x[0] + c0
    v0 = c0.run(backend=backend)
    v1 = c1.run(backend=backend)
    assert abs(abs(v0[0]) - 1) < 1e-8
    assert abs(v1[1] / v0[0] - val) < 1e-8

    val = np.exp(-0.5j * (phi + lambd)) / np.sqrt(2)
    c0 = Circuit().cu2(phi, lambd)[0, 1]
    c1 = Circuit().x[0] + c0
    v0 = c0.run(backend=backend)
    v1 = c1.run(backend=backend)
    assert abs(abs(v0[0]) - 1) < 1e-8
    assert abs(v1[1] / v0[0] - val) < 1e-8

    val = np.exp(-0.5j * (phi + lambd)) * np.cos(theta * 0.5)
    c0 = Circuit().cu3(theta, phi, lambd)[0, 1]
    c1 = Circuit().x[0] + c0
    v0 = c0.run(backend=backend)
    v1 = c1.run(backend=backend)
    assert abs(abs(v0[0]) - 1) < 1e-8
    assert abs(v1[1] / v0[0] - val) < 1e-8
Exemple #24
0
def test_measurement_hadamard1(backend):
    n = 1000
    c = Circuit().h[0].m[0]
    cnt = c.run(backend=backend, shots=n)
    a, b = cnt.most_common(2)
    assert a[1] + b[1] == n
    # variance of binomial distribution (n -> ∞) is np(1-p)
    # therefore, 3σ = 3 * sqrt(np(1-p))
    three_sigma = 3 * np.sqrt(n * 0.5 * 0.5)
    assert abs(a[1] - n / 2) < three_sigma
Exemple #25
0
def test_measurement3(backend):
    # 75% |0> + 25% |1>
    c = Circuit().rx(math.pi / 3)[0].m[0]
    n = 500
    cnt = c.run(backend=backend, shots=n)
    most_common = cnt.most_common(1)[0]
    assert most_common[0] == "0"
    # variance of binomial distribution (n -> ∞) is np(1-p)
    # therefore, 3σ = 3 * sqrt(np(1-p))
    three_sigma = 3 * np.sqrt(n * 0.75 * 0.25)
    assert abs(most_common[1] - 0.75 * n) < three_sigma
Exemple #26
0
def test_decomposite3(basis):
    p = random.random()
    q = random.random()
    r = random.random()
    s = random.random()

    c = Circuit().ry(p)[1].rz(q)[1].ry(r)[0].rz(s)[0].zz[0, 1].h[2].ry(
        r)[0].rz(s)[0].ry(p)[1].rz(q)[1]
    v1 = c.run()
    v2 = c.run_with_2q_decomposition(basis=basis).run()
    assert np.allclose(ignore_global_phase(v1), ignore_global_phase(v2))
Exemple #27
0
def test_decomposite3(basis):
    p = random.random()
    q = random.random()
    r = random.random()
    s = random.random()

    c = Circuit().ry(p)[1].rz(q)[1].ry(r)[0].rz(s)[0].zz[0, 1].h[2].ry(
        r)[0].rz(s)[0].ry(p)[1].rz(q)[1]
    v1 = c.run()
    v2 = c.run_with_2q_decomposition(basis=basis).run()
    assert is_vec_same(v1, v2, ignore_global='ab')
Exemple #28
0
def test_measurement3():
    # 75% |0> + 25% |1>
    c = Circuit().rx(np.pi / 3)[0].m[0]
    n = 10000
    cnt = c.run(shots=n)
    most_common = cnt.most_common(1)[0]
    assert most_common[0] == "0"
    # variance of binomial distribution (n -> ∞) is np(1-p)
    # therefore, 2σ = 2 * sqrt(np(1-p))
    two_sigma = 2 * np.sqrt(n * 0.75 * 0.25)
    assert abs(most_common[1] - 0.75 * n) < two_sigma
Exemple #29
0
def test_globalphase(backend):
    theta = 1.2
    phi = 1.6
    lambd = 2.3
    c = Circuit().rz(theta)[0]
    assert abs(c.run(backend=backend)[0] - np.exp(-0.5j * theta)) < 1e-8

    c = Circuit().phase(theta)[0]
    assert abs(c.run(backend=backend)[0] - 1) < 1e-8

    c = Circuit().u1(theta)[0]
    assert abs(c.run(backend=backend)[0] - np.exp(-0.5j * theta)) < 1e-8

    v0 = np.exp(-0.5j * (phi + lambd)) / np.sqrt(2)
    c = Circuit().u2(phi, lambd)[0]
    assert abs(c.run(backend=backend)[0] - v0) < 1e-8

    v0 = np.exp(-0.5j * (phi + lambd)) * np.cos(theta * 0.5)
    c = Circuit().u3(theta, phi, lambd)[0]
    assert abs(c.run(backend=backend)[0] - v0) < 1e-8
Exemple #30
0
def circuit_to_unitary(circ: Circuit, *runargs, **runkwargs):
    runkwargs.setdefault('returns', 'statevector')
    runkwargs.setdefault('ignore_global', False)
    n_qubits = circ.n_qubits
    vecs = []
    if n_qubits == 0:
        return np.array([[1]])
    for i in range(1 << n_qubits):
        bitmask = tuple(k for k in range(n_qubits) if (1 << k) & i)
        c = Circuit()
        if bitmask:
            c.x[bitmask]
        c += circ
        vecs.append(c.run(*runargs, **runkwargs))
    return np.array(vecs).T