Quantum $K$-theory, I: Foundations