Ensure you have Units of Measure & Packagings enabled in Settings:

Then, simply create the necessary Products; As per your example here this is Chemical A and Product B.

Ensure Chemical A is set to per kg (at Sales Price or Cost) Product B is up to you, depending what your manufacturing process is supposed to end up with:

For the Bill of Material for Product B then just add the Component Chemical A: as 0.5g (or create an additional Unit of Measure mg, smaller than g by the factor 1000 (i.e. at Sales -> Configuration -> Units & Packagings).

