From 1f5445730890a960ddc8dee3af10522e7bf7906c Mon Sep 17 00:00:00 2001 From: John Lapeyre Date: Thu, 7 Aug 2025 14:13:34 -0400 Subject: [PATCH 1/2] Calculate the default working precision from the given error tolerance This applies only the command-line program. If the user does not pass --dps, then mpmath.mp.dps is caculcated from epsilon. The heuristic used is borrowed from the Haskell version of gridsynth. --- README.md | 7 ++++--- pygridsynth/__main__.py | 9 +++++++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 9a11c68..90e1244 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,8 @@ python -m pygridsynth [options] ### Options -- `--dps`: Sets the calculation precision (default: `128`). +- `--dps`: Sets the working precision of the calculation. If `--dps` is not given, then the working precision will + be calculated from `epsilon`. - `--dtimeout`, `-dt`: Sets the timeout for solving diophantine equations in milliseconds (default: `200`). - `--ftimeout`, `-ft`: Sets the timeout for factorization in milliseconds (default: `50`). - `--verbose`, `-v`: Enables detailed output. @@ -50,7 +51,7 @@ python -m pygridsynth [options] ### Example Execution ```bash -python -m pygridsynth 0.5 1e-50 --dps 256 --verbose --time +python -m pygridsynth 0.5 1e-50 --verbose --time ``` This command will: @@ -89,4 +90,4 @@ This project is licensed under the GNU General Public License v3 or later. - Neil J. Ross and Peter Selinger. Optimal ancilla-free Clifford+T approximation of z-rotations, 2016. - Peter Selinger. Efficient Clifford+T approximation of single-qubit operators, 2014. - Peter Selinger and Neil J. Ross. Exact and approximate synthesis of quantum circuits. https://www.mathstat.dal.ca/~selinger/newsynth/, 2018. -- Vadym Kliuchnikov, Dmitri Maslov, and Michele Mosca. Fast and efficient exact synthesis of single qubit unitaries generated by Clifford and T gates, 2013. \ No newline at end of file +- Vadym Kliuchnikov, Dmitri Maslov, and Michele Mosca. Fast and efficient exact synthesis of single qubit unitaries generated by Clifford and T gates, 2013. diff --git a/pygridsynth/__main__.py b/pygridsynth/__main__.py index 0995c75..3022903 100644 --- a/pygridsynth/__main__.py +++ b/pygridsynth/__main__.py @@ -9,7 +9,7 @@ def main(): parser.add_argument('theta', type=str) parser.add_argument('epsilon', type=str) - parser.add_argument('--dps', type=int, default=128) + parser.add_argument('--dps', type=int, default=None) parser.add_argument('--dtimeout', '-dt', type=int, default=200) parser.add_argument('--ftimeout', '-ft', type=int, default=50) parser.add_argument('--verbose', '-v', action='store_true') @@ -17,10 +17,15 @@ def main(): parser.add_argument('--showgraph', '-g', action='store_true') args = parser.parse_args() + epsilon1 = mpmath.mpmathify(args.epsilon) + dps_of_result = -mpmath.log10(epsilon1) + # Use the same heuristic as Haskell gridsynth for setting dps + if args.dps is None: + args.dps = 15 + 2.5 * dps_of_result mpmath.mp.dps = args.dps + epsilon = mpmath.mpmathify(args.epsilon) mpmath.mp.pretty = True theta = mpmath.mpmathify(args.theta) - epsilon = mpmath.mpmathify(args.epsilon) gates = gridsynth_gates(theta=theta, epsilon=epsilon, verbose=args.verbose, measure_time=args.time, From d562df909f50e11fab1bf16f3318cfcea81f5d35 Mon Sep 17 00:00:00 2001 From: Nobuyuki Yoshioka <36436759+NnktYoshioka@users.noreply.github.com> Date: Wed, 13 Aug 2025 16:47:28 +0900 Subject: [PATCH 2/2] Update __main__.py mpmath.mpmathify(theta) and mpmath.mpmathify(f"{theta}") gives different results, so I updated the code. Minimum example is the following: mpmath.mp.dps = 50 print(f'\n{mpmath.mpmathify(0.3)=}') print(f'{mpmath.mpmathify("0.3")=}') #mpmath.mpmathify(0.3)=mpf('0.29999999999999998889776975374843459576368331909179688') #mpmath.mpmathify("0.3")=mpf('0.29999999999999999999999999999999999999999999999999987') --- pygridsynth/__main__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pygridsynth/__main__.py b/pygridsynth/__main__.py index 3022903..ce1168b 100644 --- a/pygridsynth/__main__.py +++ b/pygridsynth/__main__.py @@ -23,9 +23,9 @@ def main(): if args.dps is None: args.dps = 15 + 2.5 * dps_of_result mpmath.mp.dps = args.dps - epsilon = mpmath.mpmathify(args.epsilon) + epsilon = mpmath.mpmathify(f"{args.epsilon}") mpmath.mp.pretty = True - theta = mpmath.mpmathify(args.theta) + theta = mpmath.mpmathify(f"{args.theta}") gates = gridsynth_gates(theta=theta, epsilon=epsilon, verbose=args.verbose, measure_time=args.time,