As a cornerstone for many quantum linear algebraic and quantum machine learning algorithms, controlled quantum state preparation (CQSP) aims to provide the transformation of |i⟩|0n⟩→|i⟩|ψi⟩ for all i∈{0,1}k for the given n-qubit states |ψi⟩. In this paper, we construct a quantum circuit for implementing CQSP, with depth O(n+k+2n+kn+k+m) and size O(2n+k) for any given number m of ancillary qubits. These bounds, which can also be viewed as a time-space tradeoff for the transformation, are optimal for any integer parameters m,k≥0 and n≥1. When k=0, the problem becomes the canonical quantum state preparation (QSP) problem with ancillary qubits, which asks for efficient implementations of the transformation |0n⟩|0m⟩→|ψ⟩|0m⟩. This problem has many applications with many investigations, yet its circuit complexity remains open. Our construction completely solves this problem, pinning down its depth complexity to Θ(n+2n/(n+m)) and its size complexity to Θ(2n) for any m. Another fundamental problem, unitary synthesis, asks to implement a general n-qubit unitary by a quantum circuit. Previous work shows a lower bound of Ω(n+4n/(n+m)) and an upper bound of O(n2n) for m=Ω(2n/n) ancillary qubits. In this paper, we quadratically shrink this gap by presenting a quantum circuit of the depth of O(n2n/2+n1/223n/2m1/2  ).