コード例 #1
0
ファイル: test_grover.py プロジェクト: cosmosZhou/sagemath
def test_WGate():
    nqubits = 2
    basis_states = superposition_basis(nqubits)
    assert qapply(WGate(nqubits)*basis_states) == basis_states

    expected = ((2/sqrt(pow(2, nqubits)))*basis_states) - IntQubit(1, nqubits=nqubits)
    assert qapply(WGate(nqubits)*IntQubit(1, nqubits=nqubits)) == expected
コード例 #2
0
ファイル: grover_example.py プロジェクト: Daaofer/sympyTest
def main():
    print()
    print('Demonstration of Grover\'s Algorithm')
    print('The OracleGate or V Gate carries the unknown function f(x)')
    print(
        '> V|x> = ((-1)^f(x))|x> where f(x) = 1 when x = a (True in our case)')
    print('> and 0 (False in our case) otherwise')
    print()

    nqubits = 2
    print('nqubits = ', nqubits)

    v = OracleGate(nqubits, black_box)
    print('Oracle or v = OracleGate(%r, black_box)' % nqubits)
    print()

    psi = superposition_basis(nqubits)
    print('psi:')
    pprint(psi)
    demo_vgate_app(v)
    print('qapply(v*psi)')
    pprint(qapply(v * psi))
    print()

    w = WGate(nqubits)
    print('WGate or w = WGate(%r)' % nqubits)
    print('On a 2 Qubit system like psi, 1 iteration is enough to yield |1>')
    print('qapply(w*v*psi)')
    pprint(qapply(w * v * psi))
    print()

    nqubits = 3
    print('On a 3 Qubit system, it requires 2 iterations to achieve')
    print('|1> with high enough probability')
    psi = superposition_basis(nqubits)
    print('psi:')
    pprint(psi)

    v = OracleGate(nqubits, black_box)
    print('Oracle or v = OracleGate(%r, black_box)' % nqubits)
    print()

    print('iter1 = grover.grover_iteration(psi, v)')
    iter1 = qapply(grover_iteration(psi, v))
    pprint(iter1)
    print()

    print('iter2 = grover.grover_iteration(iter1, v)')
    iter2 = qapply(grover_iteration(iter1, v))
    pprint(iter2)
    print()
コード例 #3
0
ファイル: grover_example.py プロジェクト: msgoff/sympy
def main():
    print()
    print("Demonstration of Grover's Algorithm")
    print("The OracleGate or V Gate carries the unknown function f(x)")
    print(
        "> V|x> = ((-1)^f(x))|x> where f(x) = 1 when x = a (True in our case)")
    print("> and 0 (False in our case) otherwise")
    print()

    nqubits = 2
    print("nqubits = ", nqubits)

    v = OracleGate(nqubits, black_box)
    print("Oracle or v = OracleGate(%r, black_box)" % nqubits)
    print()

    psi = superposition_basis(nqubits)
    print("psi:")
    pprint(psi)
    demo_vgate_app(v)
    print("qapply(v*psi)")
    pprint(qapply(v * psi))
    print()

    w = WGate(nqubits)
    print("WGate or w = WGate(%r)" % nqubits)
    print("On a 2 Qubit system like psi, 1 iteration is enough to yield |1>")
    print("qapply(w*v*psi)")
    pprint(qapply(w * v * psi))
    print()

    nqubits = 3
    print("On a 3 Qubit system, it requires 2 iterations to achieve")
    print("|1> with high enough probability")
    psi = superposition_basis(nqubits)
    print("psi:")
    pprint(psi)

    v = OracleGate(nqubits, black_box)
    print("Oracle or v = OracleGate(%r, black_box)" % nqubits)
    print()

    print("iter1 = grover.grover_iteration(psi, v)")
    iter1 = qapply(grover_iteration(psi, v))
    pprint(iter1)
    print()

    print("iter2 = grover.grover_iteration(iter1, v)")
    iter2 = qapply(grover_iteration(iter1, v))
    pprint(iter2)
    print()
コード例 #4
0
ファイル: test_args.py プロジェクト: Visheshk/sympy
def test_sympy__physics__quantum__grover__WGate():
    from sympy.physics.quantum.grover import WGate
    assert _test_args(WGate(1))