def test_swap_gate(): """Test the SWAP gate.""" swap_gate_matrix = Matrix(((1,0,0,0),(0,0,1,0),(0,1,0,0),(0,0,0,1))) assert represent(SwapGate(1,0).decompose(), nqubits=2) == swap_gate_matrix assert qapply(SwapGate(1,3)*Qubit('0010')) == Qubit('1000') nqubits = 4 for i in range(nqubits): for j in range(i): assert represent(SwapGate(i,j), nqubits=nqubits) ==\ represent(SwapGate(i,j).decompose(), nqubits=nqubits)
def test_quantum_fourier(): assert QFT(0,3).decompose() == SwapGate(0,2)*HadamardGate(0)*CGate((0,), PhaseGate(1))\ *HadamardGate(1)*CGate((0,), TGate(2))*CGate((1,), PhaseGate(2))*HadamardGate(2) assert IQFT(0,3).decompose() == HadamardGate(2)*CGate((1,), RkGate(2,-2))*CGate((0,),RkGate(2,-3))\ *HadamardGate(1)*CGate((0,), RkGate(1,-2))*HadamardGate(0)*SwapGate(0,2) assert represent(QFT(0,3), nqubits=3)\ == Matrix([[exp(2*pi*I/8)**(i*j%8)/sqrt(8) for i in range(8)] for j in range(8)]) assert QFT(0, 4).decompose() #non-trivial decomposition assert qapply(QFT(0,3).decompose()*Qubit(0,0,0)).expand() ==\ qapply(HadamardGate(0)*HadamardGate(1)*HadamardGate(2)*Qubit(0,0,0)).expand()
def decompose(self): """Decomposes IQFT into elementary gates.""" start = self.args[0] finish = self.args[1] circuit = 1 for i in range((finish-start)//2): circuit = SwapGate(i+start, finish-i-1)*circuit for level in range(start, finish): for i in reversed(range(level-start)): circuit = CGate(level-i-1, RkGate(level, -i-2))*circuit circuit = HadamardGate(level)*circuit return circuit
def decompose(self): """Decomposes QFT into elementary gates.""" start = self.label[0] finish = self.label[1] circuit = 1 for level in reversed(range(start, finish)): circuit = HadamardGate(level) * circuit for i in range(level - start): circuit = CGate(level - i - 1, RkGate(level, i + 2)) * circuit #FIXME-py3k: TypeError: 'Rational' object cannot be interpreted as an integer for i in range((finish - start) / 2): circuit = SwapGate(i + start, finish - i - 1) * circuit return circuit
def test_sympy__physics__quantum__gate__SwapGate(): from sympy.physics.quantum.gate import SwapGate assert _test_args(SwapGate(0, 1))