def test_set_state(): q = Qubits(2) q.set_state('10') assert q.data[0, 0] == 0 assert q.data[0, 1] == 0 assert q.data[1, 0] == 1 assert q.data[1, 1] == 0 assert np.allclose( q.data.flatten(), np.array([0, 0, 1, 0]) ) q.set_state([0, 1, 0, 0]) assert q.data[0, 0] == 0 assert q.data[0, 1] == 1 assert q.data[1, 0] == 0 assert q.data[1, 1] == 0 assert np.allclose( q.data.flatten(), np.array([0, 1, 0, 0]) ) q.set_state([[0, 0], [0, 1]]) assert q.data[0, 0] == 0 assert q.data[0, 1] == 0 assert q.data[1, 0] == 0 assert q.data[1, 1] == 1 assert np.allclose( q.data, np.array([[0, 0], [0, 1]]) )
def test_gate_control_0(): q = Qubits(2) q.gate(X, target=0, control_0=1) assert q.data[0, 0] == 0 assert q.data[0, 1] == 0 assert q.data[1, 0] == 1 assert q.data[1, 1] == 0 q = Qubits(2) q.gate(X, target=1) q.gate(X, target=0, control_0=1) assert q.data[0, 0] == 0 assert q.data[0, 1] == 1 assert q.data[1, 0] == 0 assert q.data[1, 1] == 0
def test_init(): q = Qubits(1) assert q.data[0] == 1 assert q.data[1] == 0 assert np.allclose( q.data.flatten(), np.array([1, 0]) ) q = Qubits(2) assert q.data[0, 0] == 1 assert q.data[0, 1] == 0 assert q.data[1, 0] == 0 assert q.data[1, 1] == 0 assert np.allclose( q.data.flatten(), np.array([1, 0, 0, 0]) )
def test_get_state(): q = Qubits(2) q.set_state('11') assert np.allclose( q.get_state(), np.array([0, 0, 0, 1]) ) q.set_state('01') assert np.allclose( q.get_state(flatten=False), np.array([[0, 1], [0, 0]]) )
def test_gate_single_target(): q = Qubits(2) q.gate(X, target=0) assert q.data[0, 0] == 0 assert q.data[0, 1] == 0 assert q.data[1, 0] == 1 assert q.data[1, 1] == 0 assert np.allclose( q.data.flatten(), np.array([0, 0, 1, 0]) ) q = Qubits(2) q.gate(X, target=1) assert q.data[0, 0] == 0 assert q.data[0, 1] == 1 assert q.data[1, 0] == 0 assert q.data[1, 1] == 0 assert np.allclose( q.data.flatten(), np.array([0, 1, 0, 0]) )
#print("HS {}".format(i)) q.gate(operator.S, target=targ) q.gate(operator.H, target=targ) #print("CNOT({}, {})".format(i, target)) q.gate(operator.X, control=targ, target=cnot_target) q.gate(operator.rz(2 * theta), target=cnot_target) for target in target_list[:-1]: targ = int(target) q.gate(operator.X, control=targ, target=cnot_target) if op[targ] == "X": q.gate(operator.H, target=targ) elif op[targ] == "Y": q.gate(operator.H, target=targ) q.gate(operator.Sdag, target=targ) if op[cnot_target] == "X": q.gate(operator.H, target=cnot_target) elif op[cnot_target] == "Y": q.gate(operator.H, target=cnot_target) q.gate(operator.Sdag, target=cnot_target) if __name__ == '__main__': from qupy.qubit import Qubits q_ins = Qubits(2) q_ins.gate(operator.H, target=1) q_ins.gate(operator.H, target=0) th = np.pi / 2 paulistr = "ZY" expm_pauli(q_ins, th, paulistr) print(q_ins.get_state())
def test_gate_single_qubit(): q = Qubits(1) q.gate(Y, target=0) assert np.allclose( q.data, np.array([0, 1j]) ) q = Qubits(1) q.gate(ry(0.1), target=0) q.gate(rz(0.1), target=0) psi1 = q.data q = Qubits(1) q.gate(np.dot(rz(0.1), ry(0.1)), target=0) psi2 = q.data q = Qubits(1) q.gate(np.dot(ry(0.1), rz(0.1)), target=0) psi3 = q.data assert np.allclose(psi1, psi2) assert not np.allclose(psi1, psi3)
def test_projection(): q = Qubits(2) res0 = q.projection(target=0) res1 = q.projection(target=1) assert res0 == 0 assert res1 == 0 q = Qubits(2) q.gate(Y, target=0) res0 = q.projection(target=0) res1 = q.projection(target=1) assert res0 == 1 assert res1 == 0 q = Qubits(2) q.gate(Y, target=1) res0 = q.projection(target=0) res1 = q.projection(target=1) assert res0 == 0 assert res1 == 1 q = Qubits(2) q.gate(Y, target=0) q.gate(Y, target=1) res0 = q.projection(target=0) res1 = q.projection(target=1) assert res0 == 1 assert res1 == 1
def test_gate_multi_targets(): q = Qubits(2) q.gate(X, target=0) assert q.data[0, 0] == 0 assert q.data[0, 1] == 0 assert q.data[1, 0] == 1 assert q.data[1, 1] == 0 q.gate(swap, target=(0, 1)) assert q.data[0, 0] == 0 assert q.data[0, 1] == 1 assert q.data[1, 0] == 0 assert q.data[1, 1] == 0 q = Qubits(2) q.gate(X, target=0) q.gate(swap, target=(1, 0)) assert q.data[0, 0] == 0 assert q.data[0, 1] == 1 assert q.data[1, 0] == 0 assert q.data[1, 1] == 0
def test_swap_is_3cnot(): q = Qubits(2) q.gate(rx(0.1), target=0) q.gate(rx(0.1), target=1, control=0) q.gate(swap, target=(0, 1)) psi1 = q.data q = Qubits(2) q.gate(rx(0.1), target=0) q.gate(rx(0.1), target=1, control=0) q.gate(X, target=0, control=1) q.gate(X, target=1, control=0) q.gate(X, target=0, control=1) psi2 = q.data assert np.allclose(psi1, psi2)
def test_expect(): q = Qubits(2) res = q.expect(np.kron(I, I)) assert res == 1 q = Qubits(2) res = q.expect(np.kron(I, X)) assert res == 0 q = Qubits(2) res = q.expect({'YI': 2.5, 'II': 2, 'IX': 1.5, 'IZ': 1}) assert res == 3 q = Qubits(2) q.set_state('01') res = q.expect({'IZ': 2}) assert res == -2 q = Qubits(2) q.set_state('10') res = q.expect({'IZ': 2}) assert res == 2 q = Qubits(2) q.set_state('01') res = q.expect({'ZI': 2}) assert res == 2 q = Qubits(2) q.set_state('11') res = q.expect({'ZZ': 2}) assert res == 2