Пример #1
0
    def test_cX_gate(self):
        # test the cX/CNOT gate
        nqubits = 2
        basis0 = make_basis('00')  # basis0:|00>
        basis1 = make_basis('01')  # basis1:|10>
        basis2 = make_basis('10')  # basis2:|01>
        basis3 = make_basis('11')  # basis3:|11>
        computer = Computer(nqubits)
        CNOT = gate('CNOT', 0, 1)

        # test CNOT|00> = |00>
        computer.set_state([(basis0, 1.0)])
        computer.apply_gate(CNOT)
        coeff0 = computer.coeff(basis0)
        coeff1 = computer.coeff(basis1)
        coeff2 = computer.coeff(basis2)
        coeff3 = computer.coeff(basis3)
        assert coeff0 == approx(1.0, abs=1.0e-16)
        assert coeff1 == approx(0, abs=1.0e-16)
        assert coeff2 == approx(0, abs=1.0e-16)
        assert coeff3 == approx(0, abs=1.0e-16)

        # test CNOT|10> = |11>
        computer.set_state([(basis1, 1.0)])
        computer.apply_gate(CNOT)
        coeff0 = computer.coeff(basis0)
        coeff1 = computer.coeff(basis1)
        coeff2 = computer.coeff(basis2)
        coeff3 = computer.coeff(basis3)
        assert coeff0 == approx(0, abs=1.0e-16)
        assert coeff1 == approx(0, abs=1.0e-16)
        assert coeff2 == approx(0, abs=1.0e-16)
        assert coeff3 == approx(1, abs=1.0e-16)

        # test CNOT|01> = |01>
        computer.set_state([(basis2, 1.0)])
        computer.apply_gate(CNOT)
        coeff0 = computer.coeff(basis0)
        coeff1 = computer.coeff(basis1)
        coeff2 = computer.coeff(basis2)
        coeff3 = computer.coeff(basis3)
        assert coeff0 == approx(0, abs=1.0e-16)
        assert coeff1 == approx(0, abs=1.0e-16)
        assert coeff2 == approx(1, abs=1.0e-16)
        assert coeff3 == approx(0, abs=1.0e-16)

        # test CNOT|11> = |10>
        computer.set_state([(basis3, 1.0)])
        computer.apply_gate(CNOT)
        coeff0 = computer.coeff(basis0)
        coeff1 = computer.coeff(basis1)
        coeff2 = computer.coeff(basis2)
        coeff3 = computer.coeff(basis3)
        assert coeff0 == approx(0, abs=1.0e-16)
        assert coeff1 == approx(1, abs=1.0e-16)
        assert coeff2 == approx(0, abs=1.0e-16)
        assert coeff3 == approx(0, abs=1.0e-16)

        with pytest.raises(ValueError):
            gate('CNOT', 0, 1.0)
Пример #2
0
    def test_cY_gate(self):
        # test the cY gate
        nqubits = 2
        basis0 = make_basis('00')  # basis0:|00>
        basis1 = make_basis('01')  # basis1:|10>
        basis2 = make_basis('10')  # basis2:|01>
        basis3 = make_basis('11')  # basis3:|11>
        computer = Computer(nqubits)
        cY = gate('cY', 0, 1)

        # test cY|00> = |00>
        computer.set_state([(basis0, 1.0)])
        computer.apply_gate(cY)
        coeff0 = computer.coeff(basis0)
        coeff1 = computer.coeff(basis1)
        coeff2 = computer.coeff(basis2)
        coeff3 = computer.coeff(basis3)
        assert coeff0 == approx(1, abs=1.0e-16)
        assert coeff1 == approx(0, abs=1.0e-16)
        assert coeff2 == approx(0, abs=1.0e-16)
        assert coeff3 == approx(0, abs=1.0e-16)

        # test cY|01> = |01>
        computer.set_state([(basis2, 1.0)])
        computer.apply_gate(cY)
        coeff0 = computer.coeff(basis0)
        coeff1 = computer.coeff(basis1)
        coeff2 = computer.coeff(basis2)
        coeff3 = computer.coeff(basis3)
        assert coeff0 == approx(0, abs=1.0e-16)
        assert coeff1 == approx(0, abs=1.0e-16)
        assert coeff2 == approx(1, abs=1.0e-16)
        assert coeff3 == approx(0, abs=1.0e-16)

        # test cY|10> = i|11>
        computer.set_state([(basis1, 1.0)])
        computer.apply_gate(cY)
        coeff0 = computer.coeff(basis0)
        coeff1 = computer.coeff(basis1)
        coeff2 = computer.coeff(basis2)
        coeff3 = computer.coeff(basis3)
        assert coeff0 == approx(0, abs=1.0e-16)
        assert coeff1 == approx(0, abs=1.0e-16)
        assert coeff2 == approx(0, abs=1.0e-16)
        assert coeff3.imag == approx(1, abs=1.0e-16)

        # test cY|11> = -i|10>
        computer.set_state([(basis3, 1.0)])
        computer.apply_gate(cY)
        coeff0 = computer.coeff(basis0)
        coeff1 = computer.coeff(basis1)
        coeff2 = computer.coeff(basis2)
        coeff3 = computer.coeff(basis3)
        assert coeff0 == approx(0, abs=1.0e-16)
        assert coeff1.imag == approx(-1.0, abs=1.0e-16)
        assert coeff2 == approx(0, abs=1.0e-16)
        assert coeff3 == approx(0, abs=1.0e-16)
Пример #3
0
 def test_Z_gate(self):
     # test the Pauli Y gate
     nqubits = 1
     basis0 = make_basis('0')
     basis1 = make_basis('1')
     computer = Computer(nqubits)
     Z = gate('Z', 0, 0)
     # test Z|0> = |0>
     computer.apply_gate(Z)
     coeff0 = computer.coeff(basis0)
     coeff1 = computer.coeff(basis1)
     assert coeff0 == approx(1, abs=1.0e-16)
     assert coeff1 == approx(0, abs=1.0e-16)
     # test Z|1> = -|1>
     computer.set_state([(basis1, 1.0)])
     computer.apply_gate(Z)
     coeff0 = computer.coeff(basis0)
     coeff1 = computer.coeff(basis1)
     assert coeff0 == approx(0, abs=1.0e-16)
     assert coeff1 == approx(-1.0, abs=1.0e-16)
Пример #4
0
 def test_Y_gate(self):
     # test the Pauli Y gate
     nqubits = 1
     basis0 = make_basis('0')
     basis1 = make_basis('1')
     computer = Computer(nqubits)
     Y = gate('Y', 0, 0)
     # test Y|0> = i|1>
     computer.apply_gate(Y)
     coeff0 = computer.coeff(basis0)
     coeff1 = computer.coeff(basis1)
     assert coeff0 == approx(0, abs=1.0e-16)
     assert coeff1.imag == approx(1.0, abs=1.0 - 16)
     # test Y|1> = -i|0>
     computer.set_state([(basis1, 1.0)])
     computer.apply_gate(Y)
     coeff0 = computer.coeff(basis0)
     coeff1 = computer.coeff(basis1)
     assert coeff0.imag == approx(-1, abs=1.0e-16)
     assert coeff1 == approx(0, abs=1.0e-16)
Пример #5
0
 def test_X_gate(self):
     # test the Pauli X gate
     nqubits = 1
     basis0 = make_basis('0')
     basis1 = make_basis('1')
     computer = Computer(nqubits)
     X = gate('X', 0)
     # test X|0> = |1>
     computer.apply_gate(X)
     coeff0 = computer.coeff(basis0)
     coeff1 = computer.coeff(basis1)
     assert coeff0 == approx(0, abs=1.0e-16)
     assert coeff1 == approx(1, abs=1.0e-16)
     # test X|1> = |0>
     computer.set_state([(basis1, 1.0)])
     computer.apply_gate(X)
     coeff0 = computer.coeff(basis0)
     coeff1 = computer.coeff(basis1)
     assert coeff0 == approx(1, abs=1.0e-16)
     assert coeff1 == approx(0, abs=1.0e-16)