I have a Sonos playbar 5.1 setup - it took a while to figure it out as there isnt much help on the interwebs.
You need a soundcard for your PC that can encode Dolby Digital 5.1 on the fly. Most Creative branded soundcards can do it, I use this one as it was the cheapest I could find:
https://us.creative.com/p/sound-blaster/sound-blaster-omni-surround-5-1
Once you have the soundcard, you need to run an Optical cable from the soundcard into the Sonos playbar/beam.
Lastly enable Dolby Digital in the soundcard's software and boom it's done - you'll have 5.1 surround sound which you can also confirm using the phone app - in the Sonos phone app you can see what the input sound is - it should say "Dolby Digital 5.1"
You can then still run HDMI from the beam to the TV and use that for non PC sound. You will not be able to run everything through the TV I'm afraid - too many issues like your graphics card will not send Dolby Digital 5.1 over HDMI, it will just send multi channel uncompressed audio or 2 channel audio (because the Graphics card doesn't know about your soundbar, it will only see your 2 chanel TV and send audio for that) and your TV won't do audio encoding and will pass through whatever it gets - so your Sonos soundbar will just play two channel audio.
If there is a way to get Dolby Digital 5.1 (AC3) from a PC into your Sonos beam without using a soundcard and optical cable I'm all ears - but after a whole week of research it was the only solution I could find for Sonos products.