ported over partial trace function from old implementation