We propose a quantum-classical hybrid algorithm, named automatic quantum circuit encoding (AQCE), to encode a given arbitrarily quantum state |Ψ onto an optimal quantum circuit Ĉ with a finite number of single-and two-qubit quantum gates. The proposed algorithm employs as an objective function the absolute value of fidelity F = 0| Ĉ † |Ψ , which is maximized iteratively to construct an optimal quantum circuit Ĉ with controlled accuracy. Here, |0 is a trivial product state in the computational basis of a quantum computer. The key ingredient of the algorithm is the sequential determination of a set of optimal two-qubit unitary operators one by one via the singular value decomposition of the fidelity tensor. Once the optimal unitary operators are determined, including the location of qubits on which each unitary operator acts, elementary quantum gates are assigned algebraically. These procedures are deterministic without assuming a quantum circuit ansatz and thus do not introduce any parameter optimization of parametrized quantum gates. With noiseless numerical simulations, we demonstrate the AQCE algorithm to encode a ground state of quantum many-body systems, including the spin-1/2 antiferromagnetic Heisenberg model and the spin-1/2 XY model. The results are also compared with the quantum circuit encoding of the same quantum state onto a quantum circuit in a given circuit structure, i.e., a circuit ansatz, such as Trotter-like and MERA-like circuit structures. Moreover, we demonstrate that the AQCE algorithm can also be applied to construct an optimal quantum circuit for classical data such as a classical image that is represented as a quantum state by the amplitude encoding. This scheme allows us to flexibly vary the required quantum resource. i.e., the number of qubits, by dividing classical data into multiple pieces. Therefore, this is potentially useful for a near-term application in quantum machine learning, e.g, as a state preparation of classical data for an input quantum state to be processed. Finally, we also experimentally demonstrate that a quantum circuit generated by the AQCE algorithm can indeed represent the original quantum state reasonably on a noisy real quantum device.