A computer-implemented method of quantum circuit synthesis, comprising: determining an exact representation of a d by d unitary matrix U as a sequence of quantum gates representable as g 0,..., g n from a basis set G of available d by d basis quantum gates over a ring with involution R, wherein d is an integer greater than one, and the quantum gate representable as g I of the sequence of quantum gates is determined using a module M I = g I-1 M I-1, wherein I and n are integers and M I-1 is a module used to obtain a previous quantum gate representable by g I-1; by: identifying a torsion-free R-module L, a subset G L of stabilizing quantum gates over R that map the torsion-free R-module L to itself, and a complementary set of quantum gates G ED such that the set of quantum gates G is the union of G ED and G L, wherein quantum gates from G L are assigned a same cost and quantum gates from G ED are assigned a same cost, the cost assigned to the quantum gates from G L being lower than the cost assigned to the quantum gates from G ED; determining neighbor modules of a current module M by using a module-multiply function on the quantum gates g from G ED and the torsion-free R-module L; identifying a nearest neighbor module M' of M, wherein the nearest neighbor module M' of M is identified based on an integer vector consisting of p-adic valuations of elementary divisors of a quotient of M and an intersection of M and M'; selecting a quantum gate g from G ED that produces the nearest neighbor module M' from the current module M; and appending the quantum gate g producing the nearest neighbor module M' of M to the sequence; and implementing a quantum circuit in a quantum computer based on the sequence of quantum gates representable as g 0,..., g n. determining an exact representation of a d by d unitary matrix U as a sequence of quantum gates representable as g 0,..., g n from a basis set G of available d by d basis quantum gates over a ring with involution R, wherein d is an integer greater than one, and the quantum gate representable as g I of the sequence of quantum gates is determined using a module M I = g I-1 M I-1, wherein I and n are integers and M I-1 is a module used to obtain a previous quantum gate representable by g I-1; by: identifying a torsion-free R-module L, a subset G L of stabilizing quantum gates over R that map the torsion-free R-module L to itself, and a complementary set of quantum gates G ED such that the set of quantum gates G is the union of G ED and G L, wherein quantum gates from G L are assigned a same cost and quantum gates from G ED are assigned a same cost, the cost assigned to the quantum gates from G L being lower than the cost assigned to the quantum gates from G ED; determining neighbor modules of a current module M by using a module-multiply function on the quantum gates g from G ED and the torsion-free R-module L; identifying a nearest neighbor module M' of M, wherein the nearest neighbor module M' of M is identified based on an integer vector consisting of p-adic valuations of elementary divisors of a quotient of M and an intersection of M and M'; selecting a quantum gate g from G ED that produces the nearest neighbor module M' from the current module M; and appending the quantum gate g producing the nearest neighbor module M' of M to the sequence; and implementing a quantum circuit in a quantum computer based on the sequence of quantum gates representable as g 0,..., g n.