Quantum algorithms and simulations often require the preparation of complex states through sequences of 2-qubit gates. For a generic quantum state, the number of required gates grows exponentially with the number of qubits, becoming unfeasible on near-term quantum devices. Here, we aim at creating an approximate encoding of the target state using a limited number of gates. As a first step, we consider a quantum state that is efficiently represented classically, such as a one-dimensional matrix product state. Using tensor network techniques, we develop and implement an efficient optimization algorithm that approaches the optimal implementation, requiring a polynomial number of iterations. We, next, consider the implementation of the proposed optimization algorithm directly on a quantum computer and overcome inherent barren plateaus by employing a local cost function. Our work offers a universal method to prepare target states using local gates and represents a significant improvement over known strategies.