Bug fixed in 13.0.0

Consider the following molecule, specified by a SMILES string:

ex = "[Si+4].[Si+4].[Si+4].[O-][P]([O-])([O-])=O.[O-][P]([O-])([O-])=O.[O-][P]([O-])([O-])=O.[O-][P]([O-])([O-])=O";
m = Molecule[ex, IncludeHydrogens -> All];

Dimensions@MoleculeProperty["AdjacencyMatrix"]@m (* {23, 23} *)
AtomCount[m] (* 47 *)

The option to include hydrogens is reflected in the atom count (47), but the adjacency matrix doesn't include these atoms (only connections between 23 of the atoms are included, judging by the dimensionality of the returned matrix).

Interestingly, all of the correct bond information is present in the molecule, which would enable one to manually generate the adjacency matrix:

correct = With[
 {nAtoms = AtomCount[m],
  bonds = BondList[m][[All, 1]]},
 SparseArray[Join[bonds, Reverse /@ bonds] -> 1, {nAtoms, nAtoms}]]

A second interesting observation is that round-tripping the hydrogen-added SMILES string through Molecule a second time returns the correct (full) adjacency matrix:

export = MoleculeProperty["SMILES"]@m;
roundtrip = MoleculeProperty["AdjacencyMatrix"]@ Molecule[export, IncludeHydrogens -> All];
Dimensions[roundtrip] (* {47, 47} *)

correct == roundTrip (* True *)

Question: What's gone awry in the first example? And why might a round-trip fix it?

$Version (* 12.3.1 for Mac OS X x86 (64-bit) (July 7, 2021) *)

Added 14 Dec 2021. As noted by Jason B, below, this is fixed in Mathematica 13 (confirmed)

  • 4
    $\begingroup$ If this were working properly you should be able to do m["AdjacencyMatrix", IncludeHydrogens -> #] & /@ {True, False}. This is fixed in the upcoming release. $\endgroup$
    – Jason B.
    Nov 27, 2021 at 22:18

1 Answer 1


All molecule properties should obey the options for MoleculeValue:

In[26]:= Options[MoleculeValue, IncludeHydrogens]

Out[26]= {IncludeHydrogens -> All}

The fact that the "AdjacencyMatrix" does not respect this is a bug:

   MoleculeValue[Molecule["benzene"], "AdjacencyMatrix", 
    IncludeHydrogens -> #] & /@ {All, None}

Out[29]= {{12, 12}, {12, 12}}

As a workaround you can use

molAdjacencyMatrix[m_, OptionsPattern[IncludeHydrogens->All]] := AdjacencyMatrix[
    UndirectedGraph[m["EdgeRules", IncludeHydrogens -> OptionValue[IncludeHydrogens]]]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.