Andy Murray suffered his first defeat in nearly three months when he was beaten by Latvia's Ernests Gulbis in the third round of the Rogers Cup.