quantrs2-tytan 0.1.3

High-level quantum annealing interface inspired by Tytan for the QuantRS2 framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
//! Drug discovery applications: Molecular design and optimization.
//!
//! This module provides quantum optimization tools for drug discovery
//! including molecular design, lead optimization, and virtual screening.

// Sampler types available for drug discovery applications
#![allow(dead_code)]

use scirs2_core::ndarray::Array2;
use std::collections::{HashMap, HashSet};

/// Molecular design optimizer
pub struct MolecularDesignOptimizer {
    /// Target properties
    target_properties: TargetProperties,
    /// Fragment library
    fragment_library: FragmentLibrary,
    /// Scoring function
    scoring_function: ScoringFunction,
    /// Design constraints
    constraints: DesignConstraints,
    /// Optimization strategy
    strategy: OptimizationStrategy,
}

#[derive(Debug, Clone)]
pub struct TargetProperties {
    /// Target molecular weight
    pub molecular_weight: Option<(f64, f64)>, // (min, max)
    /// LogP range
    pub logp: Option<(f64, f64)>,
    /// LogS range
    pub logs: Option<(f64, f64)>,
    /// H-bond donors
    pub hbd: Option<(usize, usize)>,
    /// H-bond acceptors
    pub hba: Option<(usize, usize)>,
    /// Rotatable bonds
    pub rotatable_bonds: Option<(usize, usize)>,
    /// TPSA range
    pub tpsa: Option<(f64, f64)>,
    /// Custom descriptors
    pub custom_descriptors: HashMap<String, (f64, f64)>,
}

#[derive(Debug, Clone)]
pub struct FragmentLibrary {
    /// Available fragments
    pub fragments: Vec<MolecularFragment>,
    /// Connection rules
    pub connection_rules: ConnectionRules,
    /// Fragment frequencies in known drugs
    pub fragment_scores: HashMap<usize, f64>,
    /// Privileged scaffolds
    pub privileged_scaffolds: Vec<usize>,
}

#[derive(Debug, Clone)]
pub struct MolecularFragment {
    /// Fragment ID
    pub id: usize,
    /// SMILES representation
    pub smiles: String,
    /// Attachment points
    pub attachment_points: Vec<AttachmentPoint>,
    /// Fragment properties
    pub properties: FragmentProperties,
    /// Pharmacophore features
    pub pharmacophores: Vec<PharmacophoreFeature>,
}

#[derive(Debug, Clone)]
pub struct AttachmentPoint {
    /// Atom index
    pub atom_idx: usize,
    /// Bond type allowed
    pub bond_types: Vec<BondType>,
    /// Directionality
    pub direction: Vec3D,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BondType {
    Single,
    Double,
    Triple,
    Aromatic,
}

#[derive(Debug, Clone)]
pub struct Vec3D {
    pub x: f64,
    pub y: f64,
    pub z: f64,
}

#[derive(Debug, Clone)]
pub struct FragmentProperties {
    /// Molecular weight contribution
    pub mw_contribution: f64,
    /// LogP contribution
    pub logp_contribution: f64,
    /// H-bond donors
    pub hbd_count: usize,
    /// H-bond acceptors
    pub hba_count: usize,
    /// Rotatable bonds
    pub rotatable_count: usize,
    /// TPSA contribution
    pub tpsa_contribution: f64,
}

#[derive(Debug, Clone)]
pub struct PharmacophoreFeature {
    /// Feature type
    pub feature_type: PharmacophoreType,
    /// Position relative to fragment
    pub position: Vec3D,
    /// Tolerance radius
    pub tolerance: f64,
}

#[derive(Debug, Clone)]
pub enum PharmacophoreType {
    HBondDonor,
    HBondAcceptor,
    Hydrophobic,
    Aromatic,
    PositiveCharge,
    NegativeCharge,
    MetalCoordination,
}

#[derive(Debug, Clone)]
pub struct ConnectionRules {
    /// Compatible fragment pairs
    pub compatible_pairs: HashMap<(usize, usize), f64>,
    /// Forbidden connections
    pub forbidden_connections: HashSet<(usize, usize)>,
    /// Reaction templates
    pub reaction_templates: Vec<ReactionTemplate>,
}

#[derive(Debug, Clone)]
pub struct ReactionTemplate {
    /// Template name
    pub name: String,
    /// Required functional groups
    pub reactants: Vec<FunctionalGroup>,
    /// Product pattern
    pub product_pattern: String,
    /// Reaction feasibility score
    pub feasibility: f64,
}

#[derive(Debug, Clone)]
pub struct FunctionalGroup {
    /// SMARTS pattern
    pub smarts: String,
    /// Required count
    pub count: usize,
}

#[derive(Debug, Clone)]
pub enum ScoringFunction {
    /// Simple additive scoring
    Additive { weights: HashMap<String, f64> },
    /// Machine learning based
    MLBased { model_path: String },
    /// Docking score
    DockingBased { receptor: ProteinStructure },
    /// Multi-objective
    MultiObjective { objectives: Vec<ObjectiveFunction> },
    /// Pharmacophore matching
    PharmacophoreMatching { reference: PharmacophoreModel },
}

#[derive(Debug, Clone)]
pub struct ProteinStructure {
    /// PDB ID or path
    pub pdb_id: String,
    /// Active site residues
    pub active_site: Vec<usize>,
    /// Grid box for docking
    pub grid_box: GridBox,
}

#[derive(Debug, Clone)]
pub struct GridBox {
    pub center: Vec3D,
    pub dimensions: Vec3D,
    pub spacing: f64,
}

#[derive(Debug, Clone)]
pub struct PharmacophoreModel {
    /// Required features
    pub features: Vec<PharmacophoreFeature>,
    /// Distance constraints
    pub distance_constraints: Vec<DistanceConstraint>,
    /// Angle constraints
    pub angle_constraints: Vec<AngleConstraint>,
}

#[derive(Debug, Clone)]
pub struct DistanceConstraint {
    pub feature1: usize,
    pub feature2: usize,
    pub min_distance: f64,
    pub max_distance: f64,
}

#[derive(Debug, Clone)]
pub struct AngleConstraint {
    pub feature1: usize,
    pub feature2: usize,
    pub feature3: usize,
    pub min_angle: f64,
    pub max_angle: f64,
}

#[derive(Debug, Clone)]
pub enum ObjectiveFunction {
    /// Binding affinity
    BindingAffinity { weight: f64 },
    /// Synthetic accessibility
    SyntheticAccessibility { weight: f64 },
    /// ADMET properties
    ADMET {
        property: ADMETProperty,
        weight: f64,
    },
    /// Novelty
    Novelty {
        reference_set: Vec<String>,
        weight: f64,
    },
    /// Diversity
    Diversity { weight: f64 },
}

#[derive(Debug, Clone)]
pub enum ADMETProperty {
    Absorption,
    Distribution,
    Metabolism,
    Excretion,
    Toxicity,
    Solubility,
    Permeability,
    Stability,
}

#[derive(Debug, Clone)]
pub struct DesignConstraints {
    /// Maximum molecular weight
    pub max_mw: f64,
    /// Lipinski's rule of five
    pub lipinski: bool,
    /// Veber's rules
    pub veber: bool,
    /// PAINS filters
    pub pains_filter: bool,
    /// Synthetic accessibility threshold
    pub max_sa_score: f64,
    /// Minimum QED score
    pub min_qed: f64,
    /// Custom SMARTS filters
    pub smarts_filters: Vec<String>,
}

#[derive(Debug, Clone)]
pub enum OptimizationStrategy {
    /// Fragment growing
    FragmentGrowing { core: MolecularFragment },
    /// Fragment linking
    FragmentLinking { fragments: Vec<MolecularFragment> },
    /// Fragment hopping
    FragmentHopping { scaffold: MolecularFragment },
    /// De novo design
    DeNovo,
    /// Lead optimization
    LeadOptimization { lead: String },
}

impl MolecularDesignOptimizer {
    /// Create new molecular design optimizer
    pub fn new(target_properties: TargetProperties, fragment_library: FragmentLibrary) -> Self {
        Self {
            target_properties,
            fragment_library,
            scoring_function: ScoringFunction::Additive {
                weights: Self::default_weights(),
            },
            constraints: DesignConstraints::default(),
            strategy: OptimizationStrategy::DeNovo,
        }
    }

    /// Default scoring weights
    fn default_weights() -> HashMap<String, f64> {
        let mut weights = HashMap::new();
        weights.insert("mw_penalty".to_string(), -0.1);
        weights.insert("logp_penalty".to_string(), -0.2);
        weights.insert("hbd_penalty".to_string(), -0.1);
        weights.insert("hba_penalty".to_string(), -0.1);
        weights.insert("rotatable_penalty".to_string(), -0.05);
        weights.insert("tpsa_penalty".to_string(), -0.1);
        weights.insert("fragment_score".to_string(), 1.0);
        weights
    }

    /// Set scoring function
    pub fn with_scoring(mut self, scoring: ScoringFunction) -> Self {
        self.scoring_function = scoring;
        self
    }

    /// Set constraints
    pub fn with_constraints(mut self, constraints: DesignConstraints) -> Self {
        self.constraints = constraints;
        self
    }

    /// Set optimization strategy
    pub fn with_strategy(mut self, strategy: OptimizationStrategy) -> Self {
        self.strategy = strategy;
        self
    }

    /// Build QUBO for molecular design
    pub fn build_qubo(&self) -> Result<(Array2<f64>, HashMap<String, usize>), String> {
        match &self.strategy {
            OptimizationStrategy::FragmentGrowing { core } => {
                self.build_fragment_growing_qubo(core)
            }
            OptimizationStrategy::DeNovo => self.build_de_novo_qubo(),
            _ => Err("Strategy not yet implemented".to_string()),
        }
    }

    /// Build QUBO for fragment growing
    fn build_fragment_growing_qubo(
        &self,
        core: &MolecularFragment,
    ) -> Result<(Array2<f64>, HashMap<String, usize>), String> {
        // Variables: x_{f,p} = 1 if fragment f is attached at position p
        let positions = core.attachment_points.len();
        let fragments = self.fragment_library.fragments.len();
        let n_vars = positions * fragments;

        let mut qubo = Array2::zeros((n_vars, n_vars));
        let mut var_map = HashMap::new();

        // Create variable mapping
        for p in 0..positions {
            for f in 0..fragments {
                let var_name = format!("x_{f}_{p}");
                var_map.insert(var_name, p * fragments + f);
            }
        }

        // Add scoring terms
        self.add_fragment_scores(&mut qubo, &var_map, core)?;

        // Add property constraints
        self.add_property_constraints(&mut qubo, &var_map, core)?;

        // Add connection compatibility
        self.add_connection_compatibility(&mut qubo, &var_map, core)?;

        // At most one fragment per position
        self.add_uniqueness_constraints(&mut qubo, &var_map, positions, fragments)?;

        Ok((qubo, var_map))
    }

    /// Build QUBO for de novo design
    fn build_de_novo_qubo(&self) -> Result<(Array2<f64>, HashMap<String, usize>), String> {
        // Variables: x_{f,i} = 1 if fragment f is at position i in molecule
        let max_positions = 10; // Maximum molecule size
        let fragments = self.fragment_library.fragments.len();

        // Additional variables for connections
        // y_{i,j} = 1 if position i connects to position j

        let position_vars = max_positions * fragments;
        let connection_vars = max_positions * (max_positions - 1) / 2;
        let n_vars = position_vars + connection_vars;

        let mut qubo = Array2::zeros((n_vars, n_vars));
        let mut var_map = HashMap::new();

        // Position variables
        for i in 0..max_positions {
            for f in 0..fragments {
                let var_name = format!("x_{f}_{i}");
                var_map.insert(var_name, i * fragments + f);
            }
        }

        // Connection variables
        let mut var_idx = position_vars;
        for i in 0..max_positions {
            for j in i + 1..max_positions {
                let var_name = format!("y_{i}_{j}");
                var_map.insert(var_name, var_idx);
                var_idx += 1;
            }
        }

        // Add de novo specific terms
        self.add_de_novo_objective(&mut qubo, &var_map, max_positions)?;

        // Connectivity constraints
        self.add_connectivity_constraints(&mut qubo, &var_map, max_positions)?;

        // Property constraints
        self.add_global_property_constraints(&mut qubo, &var_map, max_positions)?;

        Ok((qubo, var_map))
    }

    /// Add fragment scoring terms
    fn add_fragment_scores(
        &self,
        qubo: &mut Array2<f64>,
        var_map: &HashMap<String, usize>,
        core: &MolecularFragment,
    ) -> Result<(), String> {
        let positions = core.attachment_points.len();

        for p in 0..positions {
            for f in 0..self.fragment_library.fragments.len() {
                let var_name = format!("x_{f}_{p}");
                if let Some(&var_idx) = var_map.get(&var_name) {
                    // Fragment score
                    let score = self
                        .fragment_library
                        .fragment_scores
                        .get(&f)
                        .unwrap_or(&0.0);

                    // Compatibility with attachment point
                    let compatibility = self.compute_attachment_compatibility(
                        &core.attachment_points[p],
                        &self.fragment_library.fragments[f],
                    );

                    qubo[[var_idx, var_idx]] -= score * compatibility;
                }
            }
        }

        Ok(())
    }

    /// Compute attachment compatibility
    fn compute_attachment_compatibility(
        &self,
        attachment: &AttachmentPoint,
        fragment: &MolecularFragment,
    ) -> f64 {
        // Check if fragment has compatible attachment points
        let compatible = fragment.attachment_points.iter().any(|frag_attach| {
            attachment
                .bond_types
                .iter()
                .any(|bt| frag_attach.bond_types.contains(bt))
        });

        if compatible {
            1.0
        } else {
            0.0
        }
    }

    /// Add property constraints
    fn add_property_constraints(
        &self,
        qubo: &mut Array2<f64>,
        var_map: &HashMap<String, usize>,
        core: &MolecularFragment,
    ) -> Result<(), String> {
        let penalty = 100.0;

        // Molecular weight constraint
        if let Some((min_mw, max_mw)) = self.target_properties.molecular_weight {
            let core_mw = core.properties.mw_contribution;

            for f in 0..self.fragment_library.fragments.len() {
                let frag_mw = self.fragment_library.fragments[f]
                    .properties
                    .mw_contribution;
                let total_mw = core_mw + frag_mw;

                if total_mw < min_mw || total_mw > max_mw {
                    // Penalize out-of-range combinations
                    for p in 0..core.attachment_points.len() {
                        let var_name = format!("x_{f}_{p}");
                        if let Some(&var_idx) = var_map.get(&var_name) {
                            qubo[[var_idx, var_idx]] += penalty;
                        }
                    }
                }
            }
        }

        // Similar constraints for other properties
        self.add_logp_constraints(qubo, var_map, core, penalty)?;
        self.add_hbond_constraints(qubo, var_map, core, penalty)?;

        Ok(())
    }

    /// Add LogP constraints
    fn add_logp_constraints(
        &self,
        qubo: &mut Array2<f64>,
        var_map: &HashMap<String, usize>,
        core: &MolecularFragment,
        penalty: f64,
    ) -> Result<(), String> {
        if let Some((min_logp, max_logp)) = self.target_properties.logp {
            let core_logp = core.properties.logp_contribution;

            for f in 0..self.fragment_library.fragments.len() {
                let frag_logp = self.fragment_library.fragments[f]
                    .properties
                    .logp_contribution;
                let total_logp = core_logp + frag_logp;

                if total_logp < min_logp || total_logp > max_logp {
                    for p in 0..core.attachment_points.len() {
                        let var_name = format!("x_{f}_{p}");
                        if let Some(&var_idx) = var_map.get(&var_name) {
                            qubo[[var_idx, var_idx]] += penalty * 0.5;
                        }
                    }
                }
            }
        }

        Ok(())
    }

    /// Add H-bond constraints
    fn add_hbond_constraints(
        &self,
        qubo: &mut Array2<f64>,
        var_map: &HashMap<String, usize>,
        core: &MolecularFragment,
        penalty: f64,
    ) -> Result<(), String> {
        // H-bond donor constraints
        if let Some((min_hbd, max_hbd)) = self.target_properties.hbd {
            let core_hbd = core.properties.hbd_count;

            for f in 0..self.fragment_library.fragments.len() {
                let frag_hbd = self.fragment_library.fragments[f].properties.hbd_count;
                let total_hbd = core_hbd + frag_hbd;

                if total_hbd < min_hbd || total_hbd > max_hbd {
                    for p in 0..core.attachment_points.len() {
                        let var_name = format!("x_{f}_{p}");
                        if let Some(&var_idx) = var_map.get(&var_name) {
                            qubo[[var_idx, var_idx]] += penalty * 0.3;
                        }
                    }
                }
            }
        }

        Ok(())
    }

    /// Add connection compatibility
    fn add_connection_compatibility(
        &self,
        qubo: &mut Array2<f64>,
        var_map: &HashMap<String, usize>,
        core: &MolecularFragment,
    ) -> Result<(), String> {
        let positions = core.attachment_points.len();

        // Penalize incompatible fragment pairs at different positions
        for p1 in 0..positions {
            for p2 in p1 + 1..positions {
                for f1 in 0..self.fragment_library.fragments.len() {
                    for f2 in 0..self.fragment_library.fragments.len() {
                        let var1 = format!("x_{f1}_{p1}");
                        let var2 = format!("x_{f2}_{p2}");

                        if let (Some(&idx1), Some(&idx2)) = (var_map.get(&var1), var_map.get(&var2))
                        {
                            // Check compatibility
                            if self
                                .fragment_library
                                .connection_rules
                                .forbidden_connections
                                .contains(&(f1, f2))
                            {
                                qubo[[idx1, idx2]] += 1000.0;
                            } else if let Some(&score) = self
                                .fragment_library
                                .connection_rules
                                .compatible_pairs
                                .get(&(f1, f2))
                            {
                                qubo[[idx1, idx2]] -= score;
                            }
                        }
                    }
                }
            }
        }

        Ok(())
    }

    /// Add uniqueness constraints
    fn add_uniqueness_constraints(
        &self,
        qubo: &mut Array2<f64>,
        var_map: &HashMap<String, usize>,
        positions: usize,
        fragments: usize,
    ) -> Result<(), String> {
        let penalty = 100.0;

        // At most one fragment per position
        for p in 0..positions {
            // (sum_f x_{f,p} - 1)^2 if we want exactly one
            // or just penalize multiple selections
            for f1 in 0..fragments {
                for f2 in f1 + 1..fragments {
                    let var1 = format!("x_{f1}_{p}");
                    let var2 = format!("x_{f2}_{p}");

                    if let (Some(&idx1), Some(&idx2)) = (var_map.get(&var1), var_map.get(&var2)) {
                        qubo[[idx1, idx2]] += penalty;
                    }
                }
            }
        }

        Ok(())
    }

    /// Add de novo objective
    fn add_de_novo_objective(
        &self,
        qubo: &mut Array2<f64>,
        var_map: &HashMap<String, usize>,
        max_positions: usize,
    ) -> Result<(), String> {
        // Favor molecules with good fragment scores
        for i in 0..max_positions {
            for f in 0..self.fragment_library.fragments.len() {
                let var_name = format!("x_{f}_{i}");
                if let Some(&var_idx) = var_map.get(&var_name) {
                    let score = self
                        .fragment_library
                        .fragment_scores
                        .get(&f)
                        .unwrap_or(&0.0);
                    qubo[[var_idx, var_idx]] -= score;

                    // Privileged scaffolds get bonus
                    if self.fragment_library.privileged_scaffolds.contains(&f) {
                        qubo[[var_idx, var_idx]] -= 2.0;
                    }
                }
            }
        }

        // Favor connected molecules
        for i in 0..max_positions {
            for j in i + 1..max_positions {
                let conn_var = format!("y_{i}_{j}");
                if let Some(&conn_idx) = var_map.get(&conn_var) {
                    // Small penalty for connections (want some but not too many)
                    qubo[[conn_idx, conn_idx]] += 0.1;
                }
            }
        }

        Ok(())
    }

    /// Add connectivity constraints
    fn add_connectivity_constraints(
        &self,
        qubo: &mut Array2<f64>,
        var_map: &HashMap<String, usize>,
        max_positions: usize,
    ) -> Result<(), String> {
        let penalty = 100.0;

        // If position i has a fragment, it should connect to at least one other
        for i in 0..max_positions {
            // Connection indicator for position i
            for f in 0..self.fragment_library.fragments.len() {
                let frag_var = format!("x_{f}_{i}");
                if let Some(&frag_idx) = var_map.get(&frag_var) {
                    // Must have at least one connection if fragment present
                    let mut _has_connection = false;
                    for j in 0..max_positions {
                        if i != j {
                            let conn_var = if i < j {
                                format!("y_{i}_{j}")
                            } else {
                                format!("y_{j}_{i}")
                            };

                            if let Some(&conn_idx) = var_map.get(&conn_var) {
                                // Fragment at i but no connections is penalized
                                qubo[[frag_idx, frag_idx]] += penalty;
                                qubo[[frag_idx, conn_idx]] -= penalty;
                                _has_connection = true;
                            }
                        }
                    }
                }
            }
        }

        // Connection compatibility
        self.add_connection_compatibility_de_novo(qubo, var_map, max_positions)?;

        Ok(())
    }

    /// Add connection compatibility for de novo
    fn add_connection_compatibility_de_novo(
        &self,
        qubo: &mut Array2<f64>,
        var_map: &HashMap<String, usize>,
        max_positions: usize,
    ) -> Result<(), String> {
        // If positions i and j are connected, fragments must be compatible
        for i in 0..max_positions {
            for j in i + 1..max_positions {
                let conn_var = format!("y_{i}_{j}");
                if let Some(&conn_idx) = var_map.get(&conn_var) {
                    for f1 in 0..self.fragment_library.fragments.len() {
                        for f2 in 0..self.fragment_library.fragments.len() {
                            let var1 = format!("x_{f1}_{i}");
                            let var2 = format!("x_{f2}_{j}");

                            if let (Some(&idx1), Some(&idx2)) =
                                (var_map.get(&var1), var_map.get(&var2))
                            {
                                if self
                                    .fragment_library
                                    .connection_rules
                                    .forbidden_connections
                                    .contains(&(f1, f2))
                                {
                                    // Penalize: connection + incompatible fragments
                                    // This is a 3-way interaction, approximate with 2-way
                                    qubo[[conn_idx, idx1]] += 50.0;
                                    qubo[[conn_idx, idx2]] += 50.0;
                                }
                            }
                        }
                    }
                }
            }
        }

        Ok(())
    }

    /// Add global property constraints
    fn add_global_property_constraints(
        &self,
        qubo: &mut Array2<f64>,
        var_map: &HashMap<String, usize>,
        max_positions: usize,
    ) -> Result<(), String> {
        // This is challenging as properties are additive over all selected fragments
        // Use penalty approximation

        let penalty = 10.0;

        // Approximate molecular weight constraint
        if let Some((_min_mw, max_mw)) = self.target_properties.molecular_weight {
            for i in 0..max_positions {
                for f in 0..self.fragment_library.fragments.len() {
                    let var_name = format!("x_{f}_{i}");
                    if let Some(&var_idx) = var_map.get(&var_name) {
                        let mw = self.fragment_library.fragments[f]
                            .properties
                            .mw_contribution;

                        // Penalize if single fragment already exceeds limits
                        if mw > max_mw {
                            qubo[[var_idx, var_idx]] += penalty * 10.0;
                        }

                        // Soft penalty based on contribution
                        let mw_penalty = if mw > max_mw / max_positions as f64 {
                            (mw - max_mw / max_positions as f64) * penalty
                        } else {
                            0.0
                        };

                        qubo[[var_idx, var_idx]] += mw_penalty;
                    }
                }
            }
        }

        Ok(())
    }

    /// Decode solution to molecule
    pub fn decode_solution(
        &self,
        solution: &HashMap<String, bool>,
    ) -> Result<DesignedMolecule, String> {
        match &self.strategy {
            OptimizationStrategy::FragmentGrowing { core } => {
                self.decode_fragment_growing(solution, core)
            }
            OptimizationStrategy::DeNovo => self.decode_de_novo(solution),
            _ => Err("Decoding not implemented for this strategy".to_string()),
        }
    }

    /// Decode fragment growing solution
    fn decode_fragment_growing(
        &self,
        solution: &HashMap<String, bool>,
        core: &MolecularFragment,
    ) -> Result<DesignedMolecule, String> {
        let mut fragments = vec![core.clone()];
        let mut connections = Vec::new();

        // Find attached fragments
        for (var_name, &value) in solution {
            if value && var_name.starts_with("x_") {
                let parts: Vec<&str> = var_name[2..].split('_').collect();
                if parts.len() == 2 {
                    let frag_idx: usize = parts[0].parse().unwrap_or(0);
                    let pos_idx: usize = parts[1].parse().unwrap_or(0);

                    if frag_idx < self.fragment_library.fragments.len() {
                        fragments.push(self.fragment_library.fragments[frag_idx].clone());
                        connections.push(Connection {
                            from_fragment: 0,
                            from_attachment: pos_idx,
                            to_fragment: fragments.len() - 1,
                            to_attachment: 0,
                            bond_type: BondType::Single,
                        });
                    }
                }
            }
        }

        let properties = self.calculate_properties(&fragments);
        let score = self.calculate_score(&fragments, &connections);

        Ok(DesignedMolecule {
            fragments,
            connections,
            properties,
            score,
            smiles: None, // Would need to construct SMILES
        })
    }

    /// Decode de novo solution
    fn decode_de_novo(&self, solution: &HashMap<String, bool>) -> Result<DesignedMolecule, String> {
        let mut fragment_positions: HashMap<usize, usize> = HashMap::new();
        let mut connections = Vec::new();

        // Find fragment positions
        for (var_name, &value) in solution {
            if value && var_name.starts_with("x_") {
                let parts: Vec<&str> = var_name[2..].split('_').collect();
                if parts.len() == 2 {
                    let frag_idx: usize = parts[0].parse().unwrap_or(0);
                    let pos_idx: usize = parts[1].parse().unwrap_or(0);
                    fragment_positions.insert(pos_idx, frag_idx);
                }
            }
        }

        // Find connections
        for (var_name, &value) in solution {
            if value && var_name.starts_with("y_") {
                let parts: Vec<&str> = var_name[2..].split('_').collect();
                if parts.len() == 2 {
                    let pos1: usize = parts[0].parse().unwrap_or(0);
                    let pos2: usize = parts[1].parse().unwrap_or(0);

                    if fragment_positions.contains_key(&pos1)
                        && fragment_positions.contains_key(&pos2)
                    {
                        connections.push(Connection {
                            from_fragment: pos1,
                            from_attachment: 0,
                            to_fragment: pos2,
                            to_attachment: 0,
                            bond_type: BondType::Single,
                        });
                    }
                }
            }
        }

        // Build fragment list
        let fragments: Vec<_> = fragment_positions
            .iter()
            .map(|(_, &frag_idx)| self.fragment_library.fragments[frag_idx].clone())
            .collect();

        Ok(DesignedMolecule {
            fragments,
            connections,
            properties: MolecularProperties::default(),
            score: 0.0,
            smiles: None,
        })
    }

    /// Calculate molecular properties
    fn calculate_properties(&self, fragments: &[MolecularFragment]) -> MolecularProperties {
        let mut props = MolecularProperties::default();

        for fragment in fragments {
            props.molecular_weight += fragment.properties.mw_contribution;
            props.logp += fragment.properties.logp_contribution;
            props.hbd += fragment.properties.hbd_count;
            props.hba += fragment.properties.hba_count;
            props.rotatable_bonds += fragment.properties.rotatable_count;
            props.tpsa += fragment.properties.tpsa_contribution;
        }

        props
    }

    /// Calculate molecule score
    fn calculate_score(&self, fragments: &[MolecularFragment], _connections: &[Connection]) -> f64 {
        match &self.scoring_function {
            ScoringFunction::Additive { weights } => {
                let mut score = 0.0;
                let props = self.calculate_properties(fragments);

                // Property penalties
                if let Some((min, max)) = self.target_properties.molecular_weight {
                    if props.molecular_weight < min || props.molecular_weight > max {
                        score += weights.get("mw_penalty").unwrap_or(&0.0)
                            * (props.molecular_weight - f64::midpoint(min, max)).abs();
                    }
                }

                // Fragment scores
                for fragment in fragments {
                    if let Some(&frag_score) =
                        self.fragment_library.fragment_scores.get(&fragment.id)
                    {
                        score += weights.get("fragment_score").unwrap_or(&1.0) * frag_score;
                    }
                }

                score
            }
            _ => 0.0,
        }
    }
}

impl Default for DesignConstraints {
    fn default() -> Self {
        Self {
            max_mw: 500.0,
            lipinski: true,
            veber: true,
            pains_filter: true,
            max_sa_score: 6.0,
            min_qed: 0.3,
            smarts_filters: Vec::new(),
        }
    }
}

#[derive(Debug, Clone)]
pub struct Connection {
    pub from_fragment: usize,
    pub from_attachment: usize,
    pub to_fragment: usize,
    pub to_attachment: usize,
    pub bond_type: BondType,
}

#[derive(Debug, Clone)]
pub struct DesignedMolecule {
    pub fragments: Vec<MolecularFragment>,
    pub connections: Vec<Connection>,
    pub properties: MolecularProperties,
    pub score: f64,
    pub smiles: Option<String>,
}

#[derive(Debug, Clone, Default)]
pub struct MolecularProperties {
    pub molecular_weight: f64,
    pub logp: f64,
    pub logs: f64,
    pub hbd: usize,
    pub hba: usize,
    pub rotatable_bonds: usize,
    pub tpsa: f64,
    pub sa_score: f64,
    pub qed_score: f64,
}

/// Lead optimization
pub struct LeadOptimizer {
    /// Starting lead compound
    lead_compound: String,
    /// Optimization objectives
    objectives: Vec<OptimizationObjective>,
    /// Allowed modifications
    modifications: AllowedModifications,
    /// ADMET predictor
    admet_predictor: ADMETPredictor,
}

#[derive(Debug, Clone)]
pub enum OptimizationObjective {
    /// Improve potency
    Potency { target_ic50: f64 },
    /// Improve selectivity
    Selectivity { off_targets: Vec<String> },
    /// Improve ADMET
    ADMET { properties: Vec<ADMETProperty> },
    /// Reduce molecular weight
    ReduceMW { target_mw: f64 },
    /// Improve solubility
    Solubility { target_logs: f64 },
}

#[derive(Debug, Clone)]
pub struct AllowedModifications {
    /// Bioisosteric replacements
    pub bioisosteres: HashMap<String, Vec<String>>,
    /// Allowed R-group modifications
    pub r_groups: Vec<RGroupModification>,
    /// Scaffold hopping allowed
    pub scaffold_hopping: bool,
    /// Maximum similarity to lead
    pub max_similarity: f64,
}

#[derive(Debug, Clone)]
pub struct RGroupModification {
    /// Position in molecule
    pub position: String,
    /// Allowed substituents
    pub substituents: Vec<String>,
    /// Preferred properties
    pub preferred_properties: HashMap<String, f64>,
}

#[derive(Debug, Clone)]
pub struct ADMETPredictor {
    /// Prediction models
    pub models: HashMap<ADMETProperty, PredictionModel>,
    /// Experimental data
    pub experimental_data: HashMap<String, ADMETProfile>,
}

#[derive(Debug, Clone)]
pub struct PredictionModel {
    /// Model type
    pub model_type: ModelType,
    /// Model parameters
    pub parameters: Vec<f64>,
    /// Accuracy metrics
    pub accuracy: f64,
}

#[derive(Debug, Clone)]
pub enum ModelType {
    /// Random forest
    RandomForest,
    /// Neural network
    NeuralNetwork,
    /// Support vector machine
    SVM,
    /// Physics-based
    PhysicsBased,
}

#[derive(Debug, Clone)]
pub struct ADMETProfile {
    /// Absorption properties
    pub absorption: AbsorptionProfile,
    /// Distribution properties
    pub distribution: DistributionProfile,
    /// Metabolism properties
    pub metabolism: MetabolismProfile,
    /// Excretion properties
    pub excretion: ExcretionProfile,
    /// Toxicity properties
    pub toxicity: ToxicityProfile,
}

#[derive(Debug, Clone)]
pub struct AbsorptionProfile {
    pub caco2_permeability: f64,
    pub pgp_substrate: bool,
    pub pgp_inhibitor: bool,
    pub oral_bioavailability: f64,
}

#[derive(Debug, Clone)]
pub struct DistributionProfile {
    pub plasma_protein_binding: f64,
    pub vd: f64, // Volume of distribution
    pub bbb_penetration: bool,
    pub tissue_distribution: HashMap<String, f64>,
}

#[derive(Debug, Clone)]
pub struct MetabolismProfile {
    pub cyp_substrate: HashMap<String, bool>,
    pub cyp_inhibitor: HashMap<String, bool>,
    pub metabolic_stability: f64,
    pub major_metabolites: Vec<String>,
}

#[derive(Debug, Clone)]
pub struct ExcretionProfile {
    pub renal_clearance: f64,
    pub hepatic_clearance: f64,
    pub half_life: f64,
}

#[derive(Debug, Clone)]
pub struct ToxicityProfile {
    pub ld50: f64,
    pub mutagenicity: bool,
    pub hepatotoxicity: bool,
    pub cardiotoxicity: bool,
    pub herg_inhibition: f64,
}

/// Virtual screening engine
pub struct VirtualScreeningEngine {
    /// Compound library
    library: CompoundLibrary,
    /// Screening protocol
    protocol: ScreeningProtocol,
    /// Hit selection criteria
    hit_criteria: HitSelectionCriteria,
}

#[derive(Debug, Clone)]
pub struct CompoundLibrary {
    /// Library source
    pub source: LibrarySource,
    /// Number of compounds
    pub size: usize,
    /// Diversity metrics
    pub diversity: DiversityMetrics,
    /// Filters applied
    pub filters: Vec<LibraryFilter>,
}

#[derive(Debug, Clone)]
pub enum LibrarySource {
    /// Commercial vendor
    Commercial { vendor: String },
    /// FDA approved drugs
    FDAApproved,
    /// Natural products
    NaturalProducts,
    /// Fragment library
    Fragments,
    /// Virtual enumeration
    Virtual { rules: Vec<EnumerationRule> },
}

#[derive(Debug, Clone)]
pub struct DiversityMetrics {
    pub scaffold_diversity: f64,
    pub property_coverage: f64,
    pub pharmacophore_coverage: f64,
}

#[derive(Debug, Clone)]
pub enum LibraryFilter {
    /// Molecular weight range
    MolecularWeight { min: f64, max: f64 },
    /// Lipinski compliance
    Lipinski,
    /// PAINS removal
    PAINS,
    /// Custom SMARTS
    SMARTS { pattern: String, exclude: bool },
}

#[derive(Debug, Clone)]
pub struct EnumerationRule {
    /// Core scaffold
    pub scaffold: String,
    /// Variation points
    pub variation_points: Vec<VariationPoint>,
    /// Enumeration strategy
    pub strategy: EnumerationStrategy,
}

#[derive(Debug, Clone)]
pub struct VariationPoint {
    /// Position in scaffold
    pub position: String,
    /// Available building blocks
    pub building_blocks: Vec<String>,
}

#[derive(Debug, Clone)]
pub enum EnumerationStrategy {
    /// Exhaustive enumeration
    Exhaustive,
    /// Random sampling
    RandomSampling { size: usize },
    /// Focused enumeration
    Focused { criteria: Vec<String> },
}

#[derive(Debug, Clone)]
pub enum ScreeningProtocol {
    /// Structure-based
    StructureBased {
        receptor: ProteinStructure,
        docking_program: DockingProgram,
        scoring_function: String,
    },
    /// Ligand-based
    LigandBased {
        reference_ligands: Vec<String>,
        similarity_metric: SimilarityMetric,
        threshold: f64,
    },
    /// Pharmacophore-based
    PharmacaophoreBased {
        pharmacophore: PharmacophoreModel,
        tolerance: f64,
    },
    /// Machine learning
    MachineLearning {
        model: String,
        features: Vec<String>,
    },
}

#[derive(Debug, Clone)]
pub enum DockingProgram {
    AutoDock,
    Glide,
    FlexX,
    GOLD,
    Vina,
}

#[derive(Debug, Clone)]
pub enum SimilarityMetric {
    Tanimoto,
    Dice,
    Cosine,
    Euclidean,
}

#[derive(Debug, Clone)]
pub struct HitSelectionCriteria {
    /// Score threshold
    pub score_threshold: f64,
    /// Top N compounds
    pub top_n: Option<usize>,
    /// Diversity selection
    pub diversity_selection: bool,
    /// Visual inspection required
    pub manual_inspection: bool,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_molecular_design() {
        let target = TargetProperties {
            molecular_weight: Some((300.0, 500.0)),
            logp: Some((2.0, 5.0)),
            logs: None,
            hbd: Some((0, 5)),
            hba: Some((0, 10)),
            rotatable_bonds: Some((0, 10)),
            tpsa: Some((40.0, 140.0)),
            custom_descriptors: HashMap::new(),
        };

        let mut fragments = Vec::new();
        for i in 0..5 {
            fragments.push(MolecularFragment {
                id: i,
                smiles: format!("C{}O", "C".repeat(i)),
                attachment_points: vec![AttachmentPoint {
                    atom_idx: 0,
                    bond_types: vec![BondType::Single],
                    direction: Vec3D {
                        x: 1.0,
                        y: 0.0,
                        z: 0.0,
                    },
                }],
                properties: FragmentProperties {
                    mw_contribution: (i as f64).mul_add(14.0, 50.0),
                    logp_contribution: (i as f64).mul_add(0.5, 0.5),
                    hbd_count: 1,
                    hba_count: 1,
                    rotatable_count: i,
                    tpsa_contribution: 20.0,
                },
                pharmacophores: vec![],
            });
        }

        let library = FragmentLibrary {
            fragments,
            connection_rules: ConnectionRules {
                compatible_pairs: HashMap::new(),
                forbidden_connections: HashSet::new(),
                reaction_templates: vec![],
            },
            fragment_scores: HashMap::new(),
            privileged_scaffolds: vec![],
        };

        let optimizer = MolecularDesignOptimizer::new(target, library);
        let mut result = optimizer.build_qubo();
        assert!(result.is_ok());
    }

    #[test]
    fn test_fragment_growing() {
        let core = MolecularFragment {
            id: 999,
            smiles: "c1ccccc1".to_string(),
            attachment_points: vec![
                AttachmentPoint {
                    atom_idx: 0,
                    bond_types: vec![BondType::Single, BondType::Aromatic],
                    direction: Vec3D {
                        x: 1.0,
                        y: 0.0,
                        z: 0.0,
                    },
                },
                AttachmentPoint {
                    atom_idx: 3,
                    bond_types: vec![BondType::Single, BondType::Aromatic],
                    direction: Vec3D {
                        x: -1.0,
                        y: 0.0,
                        z: 0.0,
                    },
                },
            ],
            properties: FragmentProperties {
                mw_contribution: 78.0,
                logp_contribution: 2.0,
                hbd_count: 0,
                hba_count: 0,
                rotatable_count: 0,
                tpsa_contribution: 0.0,
            },
            pharmacophores: vec![PharmacophoreFeature {
                feature_type: PharmacophoreType::Aromatic,
                position: Vec3D {
                    x: 0.0,
                    y: 0.0,
                    z: 0.0,
                },
                tolerance: 1.0,
            }],
        };

        let target = TargetProperties {
            molecular_weight: Some((200.0, 400.0)),
            logp: Some((1.0, 4.0)),
            logs: None,
            hbd: Some((0, 3)),
            hba: Some((0, 6)),
            rotatable_bonds: None,
            tpsa: None,
            custom_descriptors: HashMap::new(),
        };

        let library = FragmentLibrary {
            fragments: vec![MolecularFragment {
                id: 0,
                smiles: "CCO".to_string(),
                attachment_points: vec![AttachmentPoint {
                    atom_idx: 0,
                    bond_types: vec![BondType::Single],
                    direction: Vec3D {
                        x: 1.0,
                        y: 0.0,
                        z: 0.0,
                    },
                }],
                properties: FragmentProperties {
                    mw_contribution: 45.0,
                    logp_contribution: 0.2,
                    hbd_count: 1,
                    hba_count: 1,
                    rotatable_count: 1,
                    tpsa_contribution: 20.0,
                },
                pharmacophores: vec![],
            }],
            connection_rules: ConnectionRules {
                compatible_pairs: HashMap::new(),
                forbidden_connections: HashSet::new(),
                reaction_templates: vec![],
            },
            fragment_scores: {
                let mut scores = HashMap::new();
                scores.insert(0, 1.0);
                scores
            },
            privileged_scaffolds: vec![],
        };

        let optimizer = MolecularDesignOptimizer::new(target, library)
            .with_strategy(OptimizationStrategy::FragmentGrowing { core });

        let mut result = optimizer.build_qubo();
        assert!(result.is_ok());

        let (_qubo, var_map) = result.expect("QUBO building should succeed after is_ok check");
        assert!(!var_map.is_empty());
    }
}