Пример #1
0
def test_density_add():
    ro = qf.Register()
    circ = qf.Circuit()
    circ += qf.Move(ro[0], 1)
    circ += qf.Move(ro[1], 2)
    circ += qf.Add(ro[0], ro[1])
    circ += qf.Add(ro[0], 4)
    rho = circ.evolve()
    assert rho.memory[ro[0]] == 7
Пример #2
0
def test_div():
    ro = qf.Register()
    prog = qf.Program()
    prog += qf.Move(ro[0], 4)
    prog += qf.Move(ro[1], 1)
    prog += qf.Div(ro[0], ro[1])
    prog += qf.Div(ro[0], 2)
    ket = prog.run()
    assert ket.memory[ro[0]] == 2
Пример #3
0
def test_sub():
    ro = qf.Register()
    prog = qf.Program()
    prog += qf.Move(ro[0], 1)
    prog += qf.Move(ro[1], 2)
    prog += qf.Sub(ro[0], ro[1])
    prog += qf.Sub(ro[0], 4)
    prog += qf.Neg(ro[0])
    ket = prog.run()
    assert ket.memory[ro[0]] == 5
Пример #4
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]))
Пример #5
0
def test_comparisions():
    ro = qf.Register()
    prog = qf.Program()
    prog += qf.Move(ro[0], 1)
    prog += qf.Move(ro[1], 2)
    prog += qf.EQ(('eq', 0), ro[0], ro[1])
    prog += qf.GT(('gt', 0), ro[0], ro[1])
    prog += qf.GE(('ge', 0), ro[0], ro[1])
    prog += qf.LT(('lt', 0), ro[0], ro[1])
    prog += qf.LE(('le', 0), ro[0], ro[1])
    ket = prog.run()
    assert ket.memory[('eq', 0)] == 0
    assert ket.memory[('gt', 0)] == 0
    assert ket.memory[('ge', 0)] == 0
    assert ket.memory[('lt', 0)] == 1
    assert ket.memory[('le', 0)] == 1
Пример #6
0
def test_halt():
    ro = qf.Register()
    prog = qf.Program()
    prog += qf.Move(ro[0], 0)
    prog += qf.Halt()
    prog += qf.Not(ro[0])

    ket = prog.run()
    assert ket.memory[PC] == -1
    assert ket.memory[ro[0]] == 0
Пример #7
0
def test_reset():
    ro = qf.Register()
    prog = qf.Program()
    prog += qf.Move(ro[0], 1)
    prog += qf.Call('X', params=[], qubits=[0])
    prog += qf.Reset()
    prog += qf.Measure(0, ro[1])
    ket = prog.run()

    assert ket.qubits == (0, )
    assert ket.memory[ro[0]] == 1
    assert ket.memory[ro[1]] == 0
Пример #8
0
def test_measure_until():

    prog = qf.Program()
    prog += qf.Move(('c', 2), 1)
    prog += qf.Label('redo')
    prog += qf.Call('X', [], [0])
    prog += qf.Call('H', [], [0])
    prog += qf.Measure(0, ('c', 2))
    prog += qf.JumpUnless('redo', ('c', 2))

    ket = prog.run()
    assert ket.memory[('c', 2)] == 1
Пример #9
0
def test_if():
    circ = qf.Circuit()
    c = qf.Register('c')
    circ += qf.Move(c[0], 0)
    circ += qf.Move(c[1], 1)
    circ += qf.If(qf.X(0), c[1])
    circ += qf.Measure(0, c[0])
    ket = circ.run()
    assert ket.memory[c[0]] == 1
    assert circ.evolve().memory[c[0]] == 1

    circ = qf.Circuit()
    c = qf.Register('c')
    circ += qf.Move(c[0], 0)
    circ += qf.Move(c[1], 0)
    circ += qf.If(qf.X(0), c[1])
    circ += qf.Measure(0, c[0])
    ket = circ.run()
    assert ket.memory[c[0]] == 0
    assert circ.evolve().memory[c[0]] == 0

    circ = qf.Circuit()
    c = qf.Register('c')
    circ += qf.Move(c[0], 0)
    circ += qf.Move(c[1], 0)
    circ += qf.If(qf.X(0), c[1], value=False)
    circ += qf.Measure(0, c[0])
    ket = circ.run()
    assert ket.memory[c[0]] == 1
    assert circ.evolve().memory[c[0]] == 1
Пример #10
0
def test_wait():
    ro = qf.Register()
    prog = qf.Program()
    prog += qf.Move(ro[0], 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
Пример #11
0
def test_jump():
    ro = qf.Register()
    prog = qf.Program()
    prog += qf.Move(ro[0], 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
Пример #12
0
def test_classical_moves():
    quil = "MOVE b[0] b[1]"
    cmd = qf.parse_quil(quil)[0]
    assert isinstance(cmd, qf.Move)
    assert cmd.quil() == quil

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

    # quil = "CONVERT b[0] b[1]"
    # cmd = qf.parse_quil(quil)[0]
    # assert isinstance(cmd, qf.Convert)
    # assert cmd.quil() == quil

    quil = "LOAD b[0] this b[1]"
    cmd = qf.parse_quil(quil)[0]
    assert isinstance(cmd, qf.Load)
    assert cmd.quil() == quil
    # assert len(cmd.cbits) == 2

    quil = "STORE that b[0] b[1]"
    cmd = qf.parse_quil(quil)[0]
    assert isinstance(cmd, qf.Store)
    assert cmd.quil() == quil
    # assert len(cmd.cbits) == 2

    quil = "STORE that b[0] 200"
    cmd = qf.parse_quil(quil)[0]
    assert isinstance(cmd, qf.Store)
    assert cmd.quil() == quil
    # assert len(cmd.cbits) == 1

    b = qf.Register('b')
    _test("MOVE b[0] b[1]", qf.Move(b[0], b[1]))
    _test("EXCHANGE b[0] b[1]", qf.Exchange(b[0], b[1]))
Пример #13
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}
Пример #14
0
def test_include():
    prog = qf.Program()
    prog += qf.Move(('a', 0), 0)
    instr = qf.Include('somefile.quil', qf.Program())
    assert instr.quil() == 'INCLUDE "somefile.quil"'