Пример #1
0
def test_wait():
    ro = qf.Register()
    prog = qf.Program()
    prog += qf.FALSE(ro[0])
    prog += qf.Wait()
    prog += qf.Not(ro[0])
    prog += qf.Wait()
    prog += qf.Not(ro[0])
    prog += qf.Wait()
    prog += qf.Not(ro[0])

    ket = prog.run()
    assert ket.memory[ro[0]] == 1
Пример #2
0
def test_halt():
    ro = qf.Register()
    prog = qf.Program()
    prog += qf.FALSE(ro[0])
    prog += qf.Halt()
    prog += qf.Not(ro[0])

    ket = prog.run()
    assert ket.memory[PC] == -1
    assert ket.memory[ro[0]] == 0
Пример #3
0
def test_jump():
    ro = qf.Register()
    prog = qf.Program()
    prog += qf.FALSE(ro[0])
    prog += qf.Jump('There')
    prog += qf.Not(ro[0])
    prog += qf.Label('There')
    prog += qf.Not(ro[0])
    ket = prog.run()
    assert ket.memory[ro[0]] == 1

    prog += qf.JumpWhen('There', ro[0])
    ket = prog.run()
    assert ket.memory[ro[0]] == 0

    prog += qf.Not(ro[0])
    prog += qf.JumpUnless('There', ro[0])
    ket = prog.run()
    assert ket.memory[ro[0]] == 1
Пример #4
0
def test_classical():
    ro = qf.Register()
    quil = "TRUE ro[1]"
    cmd = qf.parse_quil(quil)[0]
    assert isinstance(cmd, qf.TRUE)
    assert cmd.addr == ro[1]
    assert cmd.quil() == quil

    b = qf.Register('b')
    quil = "FALSE b[2]"
    cmd = qf.parse_quil(quil)[0]
    assert isinstance(cmd, qf.FALSE)
    assert cmd.addr == b[2]
    assert cmd.quil() == quil

    quil = "NOT b[3]"
    cmd = qf.parse_quil(quil)[0]
    assert isinstance(cmd, qf.Not)
    assert cmd.target == b[3]
    assert cmd.quil() == quil

    quil = "NEG b[3]"
    cmd = qf.parse_quil(quil)[0]
    assert isinstance(cmd, qf.Neg)
    assert cmd.target == b[3]
    assert cmd.quil() == quil

    quil = "AND b[0] b[1]"
    cmd = qf.parse_quil(quil)[0]
    assert isinstance(cmd, qf.And)
    assert cmd.quil() == quil

    quil = "IOR b[0] b[1]"
    cmd = qf.parse_quil(quil)[0]
    assert isinstance(cmd, qf.Ior)
    assert cmd.quil() == quil

    quil = "XOR b[0] b[1]"
    cmd = qf.parse_quil(quil)[0]
    assert isinstance(cmd, qf.Xor)
    assert cmd.quil() == quil

    a = qf.Register('a')
    _test("TRUE b[0]", qf.TRUE(b[0]))
    _test("FALSE b[0]", qf.FALSE(b[0]))
    _test("NOT b[0]", qf.Not(b[0]))
    _test("AND b[0] a[1]", qf.And(b[0], a[1]))
    _test("XOR b[0] b[1]", qf.Xor(b[0], b[1]))
Пример #5
0
def test_classical():
    ro = qf.Register()
    quil = "TRUE ro[1]"
    cmd = qf.forest.quil_to_program(quil)[0]
    assert isinstance(cmd, qf.Move)
    assert cmd.target == ro[1]
    assert cmd.quil() == 'MOVE ro[1] 1'

    b = qf.Register('b')
    quil = "FALSE b[2]"
    cmd = qf.forest.quil_to_program(quil)[0]
    assert isinstance(cmd, qf.Move)
    assert cmd.target == b[2]
    assert cmd.quil() == 'MOVE b[2] 0'

    quil = "NOT b[3]"
    cmd = qf.forest.quil_to_program(quil)[0]
    assert isinstance(cmd, qf.Not)
    assert cmd.target == b[3]
    assert cmd.quil() == quil

    quil = "NEG b[3]"
    cmd = qf.forest.quil_to_program(quil)[0]
    assert isinstance(cmd, qf.Neg)
    assert cmd.target == b[3]
    assert cmd.quil() == quil

    quil = "AND b[0] b[1]"
    cmd = qf.forest.quil_to_program(quil)[0]
    assert isinstance(cmd, qf.And)
    assert cmd.quil() == quil

    quil = "IOR b[0] b[1]"
    cmd = qf.forest.quil_to_program(quil)[0]
    assert isinstance(cmd, qf.Ior)
    assert cmd.quil() == quil

    quil = "XOR b[0] b[1]"
    cmd = qf.forest.quil_to_program(quil)[0]
    assert isinstance(cmd, qf.Xor)
    assert cmd.quil() == quil

    a = qf.Register('a')
    _test("TRUE b[0]", qf.Move(b[0], 1))
    _test("FALSE b[0]", qf.Move(b[0], 0))
    _test("NOT b[0]", qf.Not(b[0]))
    _test("AND b[0] a[1]", qf.And(b[0], a[1]))
    _test("XOR b[0] b[1]", qf.Xor(b[0], b[1]))
Пример #6
0
def test_logics():
    c = qf.Register('c')

    circ = qf.Circuit([qf.Move(c[0], 0),
                       qf.Move(c[1], 1),
                       qf.And(c[0], c[1])])
    # assert len(circ) == 3     # FIXME
    # assert circ.cbits == [c[0], c[1]] # FIXME

    ket = circ.run()
    assert ket.memory == {c[0]: 0, c[1]: 1}

    circ += qf.Not(c[1])
    circ += qf.And(c[0], c[1])
    ket = circ.run(ket)
    assert ket.memory == {c[0]: 0, c[1]: 0}

    circ = qf.Circuit()
    circ += qf.Move(c[0], 0)
    circ += qf.Move(c[1], 1)
    circ += qf.Ior(c[0], c[1])
    ket = circ.run()
    assert ket.memory == {c[0]: 1, c[1]: 1}

    circ = qf.Circuit()
    circ += qf.Move(c[0], 1)
    circ += qf.Move(c[1], 1)
    circ += qf.Xor(c[0], c[1])
    ket = circ.run()
    assert ket.memory == {c[0]: 0, c[1]: 1}

    circ += qf.Exchange(c[0], c[1])
    ket = circ.run(ket)
    assert ket.memory == {c[0]: 1, c[1]: 0}

    circ += qf.Move(c[0], c[1])
    ket = circ.run(ket)
    assert ket.memory == {c[0]: 0, c[1]: 0}