QuPRS 0.18.2__tar.gz → 0.18.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (112) hide show
  1. {quprs-0.18.2 → quprs-0.18.3}/PKG-INFO +1 -1
  2. {quprs-0.18.2 → quprs-0.18.3}/rust/Cargo.lock +2 -2
  3. {quprs-0.18.2 → quprs-0.18.3}/rust/Cargo.toml +1 -1
  4. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/Cargo.lock +1 -1
  5. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/build.rs +12 -0
  6. quprs-0.18.3/rust/quprs/src/wmc/count_parser_tests.rs +285 -0
  7. quprs-0.18.3/rust/quprs/src/wmc/exact_unweighted_count_tests.rs +29 -0
  8. quprs-0.18.3/rust/quprs/src/wmc/repeated_precise_weight_tests.rs +70 -0
  9. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/src/wmc.rs +3 -398
  10. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/_version.py +1 -1
  11. {quprs-0.18.2 → quprs-0.18.3}/LICENSE.md +0 -0
  12. {quprs-0.18.2 → quprs-0.18.3}/NOTICE.md +0 -0
  13. {quprs-0.18.2 → quprs-0.18.3}/README.md +0 -0
  14. {quprs-0.18.2 → quprs-0.18.3}/pyproject.toml +0 -0
  15. {quprs-0.18.2 → quprs-0.18.3}/rust/python_ffi/Cargo.toml +0 -0
  16. {quprs-0.18.2 → quprs-0.18.3}/rust/python_ffi/src/api.rs +0 -0
  17. {quprs-0.18.2 → quprs-0.18.3}/rust/python_ffi/src/circuit.rs +0 -0
  18. {quprs-0.18.2 → quprs-0.18.3}/rust/python_ffi/src/lib.rs +0 -0
  19. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/Cargo.toml +0 -0
  20. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/README.md +0 -0
  21. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/examples/codspeed_solver_native.rs +0 -0
  22. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/examples/interval_cnf_reference.rs +0 -0
  23. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/examples/solver_conformance.rs +0 -0
  24. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/native/mpfr_weight_bridge.c +0 -0
  25. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/src/browser_transport.rs +0 -0
  26. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/src/circuit.rs +0 -0
  27. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/src/diagnostics.rs +0 -0
  28. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/src/ganak-release.txt +0 -0
  29. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/src/ganak_distribution.rs +0 -0
  30. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/src/gates.rs +0 -0
  31. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/src/ir.rs +0 -0
  32. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/src/lib.rs +0 -0
  33. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/src/main.rs +0 -0
  34. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/src/pathsum/boolean_state.rs +0 -0
  35. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/src/pathsum/bra_cx.rs +0 -0
  36. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/src/pathsum/mod.rs +0 -0
  37. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/src/pathsum/phase_poly.rs +0 -0
  38. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/src/pathsum/var_manager.rs +0 -0
  39. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/src/precise_weight.rs +0 -0
  40. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/src/precision_count.rs +0 -0
  41. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/src/qasm.rs +0 -0
  42. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/src/rational.rs +0 -0
  43. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/src/rawmocs_native.rs +0 -0
  44. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/src/reduction.rs +0 -0
  45. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/src/solver_adapter.rs +0 -0
  46. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/src/solver_process.rs +0 -0
  47. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/src/solver_result.rs +0 -0
  48. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/src/stats.rs +0 -0
  49. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/src/strategy.rs +0 -0
  50. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/src/verification.rs +0 -0
  51. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/src/wmc_problem.rs +0 -0
  52. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/tests/equivalence_tests.rs +0 -0
  53. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/tests/exact_decimal_phases.rs +0 -0
  54. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/tests/external_wmc.rs +0 -0
  55. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/tests/fixtures/feynman/grover_5.qasm +0 -0
  56. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/tests/fixtures/feynman/grover_5_clifford_t.qasm +0 -0
  57. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/tests/public_api_parity.rs +0 -0
  58. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/tests/qasm_unreduced_matrix_semantics.rs +0 -0
  59. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/tests/rawmocs_native.rs +0 -0
  60. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/tests/simple_rotations.rs +0 -0
  61. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/tests/standalone_rr.rs +0 -0
  62. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/tests/test_ganak_distribution.rs +0 -0
  63. {quprs-0.18.2 → quprs-0.18.3}/rust/quprs/tests/test_wmc_tools.rs +0 -0
  64. {quprs-0.18.2 → quprs-0.18.3}/scripts/fetch_gpmc_wheel_source.sh +0 -0
  65. {quprs-0.18.2 → quprs-0.18.3}/scripts/install_gpmc_release.py +0 -0
  66. {quprs-0.18.2 → quprs-0.18.3}/scripts/install_linux_deps.sh +0 -0
  67. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/__init__.py +0 -0
  68. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/cache_manager/__init__.py +0 -0
  69. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/cache_manager/cache_decorator.py +0 -0
  70. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/cache_manager/global_cache_manager.py +0 -0
  71. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/config.py +0 -0
  72. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/interface/__init__.py +0 -0
  73. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/interface/gate_library.py +0 -0
  74. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/interface/load_qiskit.py +0 -0
  75. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/interface/ps2wmc.py +0 -0
  76. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/interface/qasm2qc.py +0 -0
  77. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/interface/rust_isolation.py +0 -0
  78. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/interface/rust_macos_process.py +0 -0
  79. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/interface/rust_resources.py +0 -0
  80. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/interface/rust_supervisor.py +0 -0
  81. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/interface/rust_worker_bootstrap.py +0 -0
  82. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/interface/rust_worker_standalone.py +0 -0
  83. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/pathsum/__init__.py +0 -0
  84. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/pathsum/core.py +0 -0
  85. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/pathsum/core.pyi +0 -0
  86. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/pathsum/gates/__init__.py +0 -0
  87. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/pathsum/gates/base.py +0 -0
  88. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/pathsum/gates/builder.py +0 -0
  89. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/pathsum/gates/multi_qubit.py +0 -0
  90. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/pathsum/gates/patcher.py +0 -0
  91. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/pathsum/gates/single_qubit.py +0 -0
  92. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/pathsum/gates/two_qubit.py +0 -0
  93. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/pathsum/pattern_match.py +0 -0
  94. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/pathsum/reduction.py +0 -0
  95. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/pathsum/statistics.py +0 -0
  96. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/py.typed +0 -0
  97. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/utils/Qiskit_Circuit_Utils.py +0 -0
  98. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/utils/__init__.py +0 -0
  99. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/utils/ganak-release.txt +0 -0
  100. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/utils/ganak_distribution.py +0 -0
  101. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/utils/gpmc-release.txt +0 -0
  102. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/utils/gpmc_cnf_metadata.py +0 -0
  103. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/utils/gpmc_distance_budget.py +0 -0
  104. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/utils/precision_count.py +0 -0
  105. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/utils/solver_adapter.py +0 -0
  106. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/utils/solver_process.py +0 -0
  107. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/utils/strategy.py +0 -0
  108. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/utils/util.py +0 -0
  109. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/utils/verification_timeout.py +0 -0
  110. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/utils/verification_tolerances.py +0 -0
  111. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/utils/wmc_problem.py +0 -0
  112. {quprs-0.18.2 → quprs-0.18.3}/src/QuPRS/utils/wmc_solvers.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: QuPRS
3
- Version: 0.18.2
3
+ Version: 0.18.3
4
4
  Classifier: Development Status :: 4 - Beta
5
5
  Classifier: Intended Audience :: Developers
6
6
  Classifier: Intended Audience :: Science/Research
@@ -1097,7 +1097,7 @@ dependencies = [
1097
1097
 
1098
1098
  [[package]]
1099
1099
  name = "quprs"
1100
- version = "0.18.2"
1100
+ version = "0.18.3"
1101
1101
  dependencies = [
1102
1102
  "anyhow",
1103
1103
  "cmake",
@@ -1121,7 +1121,7 @@ dependencies = [
1121
1121
 
1122
1122
  [[package]]
1123
1123
  name = "quprs_python"
1124
- version = "0.18.2"
1124
+ version = "0.18.3"
1125
1125
  dependencies = [
1126
1126
  "pyo3",
1127
1127
  "quprs",
@@ -5,7 +5,7 @@ members = ["quprs", "python_ffi"]
5
5
  resolver = "2"
6
6
 
7
7
  [workspace.package]
8
- version = "0.18.2"
8
+ version = "0.18.3"
9
9
  edition = "2021"
10
10
  authors = ["Wei-Jia Huang <wei.jia.huang.physics@gmail.com>"]
11
11
  publish = false
@@ -1050,7 +1050,7 @@ dependencies = [
1050
1050
 
1051
1051
  [[package]]
1052
1052
  name = "quprs"
1053
- version = "0.18.2"
1053
+ version = "0.18.3"
1054
1054
  dependencies = [
1055
1055
  "anyhow",
1056
1056
  "cmake",
@@ -88,6 +88,18 @@ fn main() {
88
88
 
89
89
  fs::create_dir_all(&dest_tools_dir).expect("Failed to create wmc_tools directory");
90
90
 
91
+ // Record the actual Cargo compiler inside the release wheel environment.
92
+ // Ordinary source and standalone crate builds do not add this payload.
93
+ println!("cargo:rerun-if-env-changed=QUPRS_RECORD_WHEEL_TOOLCHAIN");
94
+ if env::var("QUPRS_RECORD_WHEEL_TOOLCHAIN").as_deref() == Ok("1") {
95
+ let rustc = env::var_os("RUSTC").expect("Cargo compiler unavailable");
96
+ let output = std::process::Command::new(rustc).arg("-vV").output()
97
+ .expect("Cannot identify wheel build compiler");
98
+ assert!(output.status.success(), "Cannot identify wheel build compiler");
99
+ fs::write(dest_tools_dir.join("quprs-rustc.txt"), output.stdout)
100
+ .expect("Cannot retain wheel build compiler identity");
101
+ }
102
+
91
103
  // ==========================================
92
104
  // 1. Download and install Ganak
93
105
  // ==========================================
@@ -0,0 +1,285 @@
1
+ use super::{parse_real_count, solver_arguments, WmcManager, Complex};
2
+ use crate::pathsum::PathSum;
3
+ #[cfg(unix)]
4
+ use crate::solver_adapter::SolverAdapter;
5
+
6
+ #[test]
7
+ fn solvers_use_independent_floating_modes() {
8
+ assert_eq!(solver_arguments("ganak").unwrap(), &["--mode=6"]);
9
+ assert_eq!(solver_arguments("gpmc").unwrap(), &["-mode=1"]);
10
+ assert!(solver_arguments("unsupported").is_err());
11
+ }
12
+
13
+ #[test]
14
+ fn mode6_complex_scientific_output() {
15
+ use super::*;
16
+ for (value, expected) in [
17
+ ("9.28154511e-01 + 2.42960040e+10i", Complex::new(0.928154511, 2.42960040e10)),
18
+ ("-1.25E+03 - 2.5E-11i", Complex::new(-1250.0, -2.5e-11)),
19
+ ("1e-12 + -2e+03i", Complex::new(1e-12, -2000.0)),
20
+ ("+i", Complex::new(0.0, 1.0)),
21
+ ("-i", Complex::new(0.0, -1.0)),
22
+ ("1\t+\t2i", Complex::new(1.0, 2.0)),
23
+ ] {
24
+ assert_eq!(parse_count_line(&format!("c s exact quadruple float {value}")).unwrap(), Some(expected));
25
+ }
26
+ // Ganak mode 6 prefixes depend on MPFR bits; this only fixes the
27
+ // transport grammar. The value is still converted to f64.
28
+ for label in ["half", "single", "double", "quadruple", "octuple"] {
29
+ let line = format!("c s exact {label} float 1.00000000e+00 - 2.50000000e-21i");
30
+ assert_eq!(parse_count_line(&line).unwrap(), Some(Complex::new(1.0, -2.5e-21)));
31
+ }
32
+ assert!(parse_count_line("c s exact quadruple floating 1").unwrap().is_none());
33
+ assert!(parse_count_line("c s exact octuple floating 1").unwrap().is_none());
34
+ for value in ["nan", "inf", "1e999", "1 + infi", "1e-2-3e+i"] {
35
+ assert!(parse_count_line(&format!("c s exact quadruple float {value}")).is_err());
36
+ }
37
+ }
38
+
39
+
40
+ #[test]
41
+ fn parses_exact_fraction_and_scientific_notation() {
42
+ assert_eq!(parse_real_count("-3/2").unwrap(), -1.5);
43
+ assert_eq!(parse_real_count("1.25e-10").unwrap(), 1.25e-10);
44
+ }
45
+
46
+ #[test]
47
+ fn parses_fraction_with_components_larger_than_f64() {
48
+ let value = format!("1{}", "0".repeat(5000));
49
+ assert_eq!(parse_real_count(&format!("{}/{}", value, value)).unwrap(), 1.0);
50
+ }
51
+
52
+ #[test]
53
+ fn rejects_invalid_counts_instead_of_returning_zero() {
54
+ assert!(parse_real_count("malformed").is_err());
55
+ assert!(parse_real_count("1/0").is_err());
56
+ assert!(parse_real_count("inf").is_err());
57
+ }
58
+ #[test]
59
+ fn ganak_exact_complex_counts_match_hand_computed_models() {
60
+ use super::*;
61
+ for (re, im) in [(1.5, 0.25), (-1.5, -0.25), (0.0, 1.0), (0.0, 0.0), (1e-5, -2.5e-6)] {
62
+ let mut manager = WmcManager::new(&PathSum::new(0));
63
+ manager.next_var_id = 2;
64
+ manager.clauses.push(vec![1]);
65
+ manager.weights.insert(1, Complex::new(re, im));
66
+ assert_eq!(manager.solve("ganak").unwrap(), Complex::new(re, im));
67
+ }
68
+ let mut manager = WmcManager::new(&PathSum::new(0));
69
+ manager.next_var_id = 2;
70
+ manager.weights.insert(1, Complex::new(-1.0, 0.0));
71
+ assert_eq!(manager.solve("ganak").unwrap(), Complex::new(0.0, 0.0));
72
+ }
73
+
74
+ #[test]
75
+ fn concurrent_ganak_fixtures_keep_their_own_weights() {
76
+ use super::*;
77
+ use std::sync::{Arc, Barrier};
78
+
79
+ // Reproduce the parallel cargo-test pattern that can otherwise
80
+ // attribute the answer for a different CNF to this fixture. Include
81
+ // both forced and free variables, which have different exact counts.
82
+ let fixtures = [
83
+ (Complex::new(1.5, 0.25), true),
84
+ (Complex::new(-1.5, -0.25), true),
85
+ (Complex::new(0.0, 1.0), true),
86
+ (Complex::new(0.5, -0.125), true),
87
+ (Complex::new(0.25, 0.75), false),
88
+ (Complex::new(-0.5, 0.5), false),
89
+ (Complex::new(0.0, -1.0), false),
90
+ (Complex::new(1.25, -0.25), false),
91
+ ];
92
+ let barrier = Arc::new(Barrier::new(fixtures.len()));
93
+ std::thread::scope(|scope| {
94
+ let handles: Vec<_> = fixtures.into_iter().map(|(weight, forced)| {
95
+ let barrier = barrier.clone();
96
+ scope.spawn(move || {
97
+ let mut manager = WmcManager::new(&PathSum::new(0));
98
+ manager.next_var_id = 2;
99
+ if forced {
100
+ manager.clauses.push(vec![1]);
101
+ }
102
+ manager.weights.insert(1, weight);
103
+ barrier.wait();
104
+ let actual = manager.solve("ganak").unwrap();
105
+ let expected = if forced { weight } else { weight + Complex::new(1.0, 0.0) };
106
+ let error = (actual - expected).norm();
107
+ assert!(
108
+ error <= 1e-8 * expected.norm() + 1e-10,
109
+ "Cross-fixture solver result: forced={forced}, weight={weight:?}, expected={expected:?}, actual={actual:?}, error={error:e}"
110
+ );
111
+ })
112
+ }).collect();
113
+ for handle in handles {
114
+ handle.join().unwrap();
115
+ }
116
+ });
117
+ }
118
+
119
+ #[test]
120
+ fn solver_inputs_preserve_f64_roundtrip_precision() {
121
+ use super::*;
122
+ let re = -1.0 + 1e-12;
123
+ let im = -2.5e-11;
124
+ let mut manager = WmcManager::new(&PathSum::new(0));
125
+ manager.next_var_id = 2;
126
+ manager.weights.insert(1, Complex::new(re, im));
127
+ for solver in ["ganak", "gpmc"] {
128
+ let text = manager.to_dimacs_string(solver);
129
+ let line = text.lines().find(|l| l.starts_with("c p weight 1 ")).unwrap();
130
+ let fields: Vec<_> = line.split_whitespace().collect();
131
+ assert_eq!(fields[4].parse::<f64>().unwrap().to_bits(), re.to_bits(), "{solver}: {line}");
132
+ if solver == "gpmc" {
133
+ assert_eq!(fields[5].parse::<f64>().unwrap().to_bits(), im.to_bits(), "{line}");
134
+ } else {
135
+ assert_eq!(fields[6].trim_end_matches('i').parse::<f64>().unwrap().to_bits(), im.abs().to_bits(), "{line}");
136
+ assert_eq!(fields[5], "-");
137
+ }
138
+ }
139
+ }
140
+
141
+ #[test]
142
+ fn floating_solvers_match_complex_and_cancellation_fixtures() {
143
+ use super::*;
144
+ for positive in [
145
+ Complex::new(1.23456789123, -2.34567891234),
146
+ Complex::new(1e-12, -2.5e-11),
147
+ Complex::new(-1.0, 0.0),
148
+ Complex::new(-1.0 + 1e-8, 0.0),
149
+ ] {
150
+ let expected = positive + Complex::new(1.0, 0.0);
151
+ let mut manager = WmcManager::new(&PathSum::new(0));
152
+ manager.next_var_id = 2;
153
+ manager.weights.insert(1, positive);
154
+ let ganak = manager.solve("ganak").unwrap();
155
+ let gpmc = manager.solve("gpmc").unwrap();
156
+ // Keep solver-specific diagnostics: a failing numerical fixture
157
+ // must identify which executable/weight/count breached the budget.
158
+ let tolerance = 1e-8 * expected.norm() + 2e-9;
159
+ for (solver, value) in [("ganak", ganak), ("gpmc", gpmc)] {
160
+ let error = (value - expected).norm();
161
+ assert!(
162
+ error <= tolerance,
163
+ "solver={solver}, input={positive:?}, expected={expected:?}, actual={value:?}, error={error:e}, tolerance={tolerance:e}, cnf={} ",
164
+ manager.to_dimacs_string(solver),
165
+ );
166
+ }
167
+ let cross_error = (ganak - gpmc).norm();
168
+ assert!(
169
+ cross_error <= tolerance,
170
+ "ganak={ganak:?}, gpmc={gpmc:?}, input={positive:?}, cross_error={cross_error:e}, tolerance={tolerance:e}",
171
+ );
172
+ }
173
+ }
174
+
175
+ // Use an immutable system executable and per-call adapter. Writing a
176
+ // temporary executable can race with fork/exec in another test thread
177
+ // (ETXTBSY); changing the process environment can affect unrelated tests.
178
+ #[cfg(unix)]
179
+ struct FaultGpmc(&'static [&'static str]);
180
+
181
+ #[cfg(unix)]
182
+ impl crate::solver_adapter::SolverAdapter for FaultGpmc {
183
+ fn id(&self) -> &'static str { "gpmc" }
184
+ fn arguments(&self) -> &'static [&'static str] { self.0 }
185
+ fn executable_path(&self) -> String { "/bin/sh".into() }
186
+ fn capabilities(&self) -> crate::solver_adapter::SolverCapabilities {
187
+ crate::solver_adapter::GpmcAdapter.capabilities()
188
+ }
189
+ fn weight_lines(&self, var: i32, weight: Complex<f64>) -> (String, String) {
190
+ crate::solver_adapter::GpmcAdapter.weight_lines(var, weight)
191
+ }
192
+ }
193
+
194
+ #[test]
195
+ #[cfg(unix)]
196
+ fn timed_out_gpmc_solver_is_not_a_weighted_count() {
197
+ use crate::clock::{Duration, Instant};
198
+ let started = Instant::now();
199
+ let result = WmcManager::new(&PathSum::new(0)).solve_with_adapter(
200
+ &FaultGpmc(&["-c", "exec sleep 30"]),
201
+ Some(started + Duration::from_millis(250)),
202
+ );
203
+ let error = result.expect_err("hanging solver must not yield a count");
204
+ assert!(error.downcast_ref::<crate::solver_process::SolverTimeout>().is_some(),
205
+ "{error:#}");
206
+ assert!(started.elapsed() < Duration::from_secs(4));
207
+ }
208
+
209
+ #[test]
210
+ #[cfg(unix)]
211
+ fn nonzero_solver_exit_cannot_become_a_weighted_count() {
212
+ use crate::solver_result::{SolverFailure, SolverFailureKind};
213
+ use SolverFailureKind::{NonzeroExit, MissingCount, MalformedCount};
214
+ let scenarios: [(&'static [&'static str], SolverFailureKind, &str); 5] = [
215
+ (&["-c", "echo 'c s exact quadruple float 999'; exit 7"], NonzeroExit, "status="),
216
+ (&["-c", "echo 'c s exact quadruple float 999'; kill -TERM $$"], NonzeroExit, "status="),
217
+ (&["-c", "echo 'no count'"], MissingCount, "Error!"),
218
+ (&["-c", "echo 'c s exact quadruple float nan'"], MalformedCount, "Non-finite"),
219
+ (&["-c", "echo 'c s exact quadruple float malformed'"], MalformedCount, "Invalid decimal"),
220
+ ];
221
+ for (args, kind, message) in scenarios {
222
+ let result = WmcManager::new(&PathSum::new(0)).solve_with_adapter(
223
+ &FaultGpmc(args), None,
224
+ );
225
+ let error = result.expect_err("failed solver must not yield a count");
226
+ let typed = error.downcast_ref::<SolverFailure>().expect("typed solver failure");
227
+ assert_eq!(typed.kind, kind, "{error:#}");
228
+ assert_eq!(typed.solver_id, "gpmc");
229
+ assert!(error.to_string().contains(message), "{error:#}");
230
+ }
231
+ }
232
+
233
+ #[test]
234
+ fn typed_invalid_solver_does_not_dispatch() {
235
+ use crate::solver_result::{SolverFailure, SolverFailureKind};
236
+ let manager = WmcManager::new(&PathSum::new(0));
237
+ let err=manager.observe_with_deadline("missing_solver", None).unwrap_err();
238
+ let typed=err.downcast_ref::<SolverFailure>().unwrap();
239
+ assert_eq!(typed.kind, SolverFailureKind::Unsupported);
240
+ assert_eq!(typed.solver_id, "missing_solver");
241
+ }
242
+
243
+ #[test]
244
+ #[cfg(unix)]
245
+ fn typed_numeric_observation_and_negative_failures() {
246
+ use crate::solver_adapter::{SolverAdapter, SolverCapabilities, WeightGrammar};
247
+ use crate::solver_result::{SolverFailure, SolverFailureKind};
248
+ struct Mock(&'static str);
249
+ impl SolverAdapter for Mock {
250
+ fn id(&self) -> &'static str { "mock" }
251
+ fn arguments(&self) -> &'static [&'static str] {
252
+ match self.0 {
253
+ "ok" => &["-c", "printf 'c s exact quadruple float 1.5 - 0.25i\\n'"],
254
+ "nonzero" => &["-c", "echo 'c s exact quadruple float 9'; exit 7"],
255
+ "missing" => &["-c", "echo 'no weighted count'"],
256
+ "conflict" => &["-c", "printf 'c s exact quadruple float 1.5 - 0.25i\\nc s exact quadruple float 7.5 - 0.25i\\n'"],
257
+ _ => &["-c", "echo 'c s exact quadruple float nan'"],
258
+ }
259
+ }
260
+ fn capabilities(&self) -> SolverCapabilities {
261
+ SolverCapabilities { complex_weights:true,exact_count_certificate:false,grammar:WeightGrammar::Pair }
262
+ }
263
+ fn executable_path(&self) -> String { "sh".into() }
264
+ fn weight_lines(&self, var:i32, weight:Complex<f64>)->(String,String) {
265
+ crate::solver_adapter::GpmcAdapter.weight_lines(var,weight)
266
+ }
267
+ }
268
+ let manager=WmcManager::new(&PathSum::new(0));
269
+ let deadline=|| Some(crate::clock::Instant::now()+crate::clock::Duration::from_secs(10));
270
+ let selected=Mock("ok");
271
+ let result=manager.solve_with_adapter(&selected,deadline()).unwrap();
272
+ assert_eq!(result,Complex::new(1.5,-0.25));
273
+ for (case,kind) in [
274
+ ("nonzero",SolverFailureKind::NonzeroExit),
275
+ ("missing",SolverFailureKind::MissingCount),
276
+ ("malformed",SolverFailureKind::MalformedCount),
277
+ ("conflict",SolverFailureKind::MalformedCount)
278
+ ] {
279
+ let err=manager.solve_with_adapter(&Mock(case),deadline()).unwrap_err();
280
+ let typed=err.downcast_ref::<SolverFailure>().expect("category must be typed");
281
+ assert_eq!(typed.kind,kind,"case={case}");
282
+ assert_eq!(typed.solver_id,"mock");
283
+ }
284
+ }
285
+
@@ -0,0 +1,29 @@
1
+ use super::WmcManager;
2
+ use crate::pathsum::PathSum;
3
+ use num_complex::Complex;
4
+
5
+ #[test]
6
+ fn unweighted_identity_trace_has_exact_count() {
7
+ let ps = PathSum::new(1);
8
+ let mut manager = WmcManager::new(&ps);
9
+ manager.encode_trace(&ps).unwrap();
10
+ assert!(manager.has_exact_unweighted_tautology(2));
11
+ assert!(!manager.has_exact_unweighted_tautology(4));
12
+
13
+ manager.clauses.push(vec![1]);
14
+ assert!(!manager.has_exact_unweighted_tautology(2));
15
+ }
16
+
17
+ #[test]
18
+ fn negative_or_weighted_cnf_never_uses_exact_identity_shortcut() {
19
+ let ps = PathSum::new(1);
20
+ let mut manager = WmcManager::new(&ps);
21
+ manager.next_var_id = 2;
22
+ manager.clauses = vec![vec![1, -1], vec![-1, 1]];
23
+ assert!(manager.has_exact_unweighted_tautology(2));
24
+ manager.weights.insert(1, Complex::new(1.0, 0.0));
25
+ assert!(!manager.has_exact_unweighted_tautology(2));
26
+ manager.weights.clear();
27
+ manager.clauses = vec![vec![1], vec![-1]];
28
+ assert!(!manager.has_exact_unweighted_tautology(2));
29
+ }
@@ -0,0 +1,70 @@
1
+ use super::*;
2
+ use crate::rational::{PhaseCoeff, Rational};
3
+ use num_bigint::BigInt;
4
+ use num_rational::BigRational;
5
+
6
+ #[test]
7
+ fn single_source_angle_works_without_cache() {
8
+ let mut ps = PathSum::new(1);
9
+ let coeff = PhaseCoeff {
10
+ constant: Rational::new(1, 8),
11
+ radians: Some(BigRational::new(BigInt::from(1), BigInt::from(10_u8).pow(20))),
12
+ symbols: Default::default(),
13
+ };
14
+ ps.p.add_term(vec![0], coeff);
15
+ let mut manager = WmcManager::new(&ps);
16
+ manager.enable_precise_gpmc_weights(96);
17
+ manager.encode_trace(&ps).unwrap();
18
+ let precise = manager.precise_weights.as_ref().unwrap();
19
+ assert_eq!(precise.len(), 1);
20
+ let cnf = manager.to_dimacs_string("gpmc");
21
+ assert!(cnf.contains("c p weight "));
22
+ }
23
+
24
+ #[test]
25
+ fn unique_source_angles_do_not_share_precise_cnf_weights() {
26
+ let mut ps = PathSum::new(2);
27
+ let a = PhaseCoeff {
28
+ constant: Rational::new(1, 8),
29
+ radians: Some(BigRational::new(BigInt::from(1), BigInt::from(10_u8).pow(20))),
30
+ symbols: Default::default(),
31
+ };
32
+ let b = PhaseCoeff {
33
+ constant: Rational::new(1, 16),
34
+ radians: Some(BigRational::new(BigInt::from(2), BigInt::from(10_u8).pow(20))),
35
+ symbols: Default::default(),
36
+ };
37
+ ps.p.add_term(vec![0], a);
38
+ ps.p.add_term(vec![1], b);
39
+ let mut manager = WmcManager::new(&ps);
40
+ manager.enable_precise_gpmc_weights(96);
41
+ manager.encode_trace(&ps).unwrap();
42
+ let precise = manager.precise_weights.as_ref().unwrap();
43
+ assert_eq!(precise.len(), 2);
44
+ let mut values = precise.values();
45
+ assert_ne!(values.next(), values.next());
46
+ let cnf = manager.to_dimacs_string("gpmc");
47
+ assert_eq!(cnf.matches("c p weight ").count(), 4);
48
+ }
49
+
50
+ #[test]
51
+ fn identical_source_angles_have_identical_precise_cnf_weights() {
52
+ let mut ps = PathSum::new(2);
53
+ let coeff = PhaseCoeff {
54
+ constant: Rational::new(1, 8),
55
+ radians: Some(BigRational::new(BigInt::from(1), BigInt::from(10_u8).pow(20))),
56
+ symbols: Default::default(),
57
+ };
58
+ ps.p.add_term(vec![0], coeff.clone());
59
+ ps.p.add_term(vec![1], coeff);
60
+ let mut manager = WmcManager::new(&ps);
61
+ manager.enable_precise_gpmc_weights(96);
62
+ manager.encode_trace(&ps).unwrap();
63
+ let precise = manager.precise_weights.as_ref().unwrap();
64
+ assert_eq!(precise.len(), 2);
65
+ let mut rendered = precise.values();
66
+ let first = rendered.next().unwrap();
67
+ assert!(rendered.all(|weight| weight == first));
68
+ let cnf = manager.to_dimacs_string("gpmc");
69
+ assert_eq!(cnf.matches(&format!("{} {} 0", first.0, first.1)).count(), 2);
70
+ }