section_path: Biography/Career/In Ray and Hamadan
section_name: In Ray and Hamadan
paragraph_text: In Ray and Hamadan
  
  In c. 1014, Avicenna went to the city of Ray, where he entered into the service of the Buyid amir Majd al-Dawla (r. 997–1029) and his mother Sayyida Shirin, the de facto ruler of the realm. There he served as the physician at the court, treating Majd al-Dawla, who was suffering from melancholia. Avicenna reportedly later served as the "business manager" of Sayyida Shirin in Qazvin and Hamadan, though details regarding this tenure are unclear. During this period, Avicenna finished writing The Canon of Medicine and started writing his The Book of Healing.
  
  In 1015, during Avicenna's stay in Hamadan, he participated in a public debate, as was customary for newly arrived scholars in western Iran at that time. The purpose of the debate was to examine one's reputation against a prominent resident. The person whom Avicenna debated against was Abu'l-Qasim al-Kirmani, a member of the school of philosophers of Baghdad. The debate became heated, resulting in ibn Sina accusing Abu'l-Qasim of lack of basic knowledge in logic, while Abu'l-Qasim accused ibn Sina of impoliteness.
  
  After the debate, Avicenna sent a letter to the Baghdad Peripatetics, asking if Abu'l-Qasim's claim that he shared the same opinion as them was true. Abu'l-Qasim later retaliated by writing a letter to an unknown person in which he made accusations so serious that ibn Sina wrote to Abu Sa'd, the deputy of Majd al-Dawla, to investigate the matter. The accusation made towards Avicenna may have been the same as he had received earlier, in which he was accused by the people of Hamadan of copying the stylistic structures of the Quran in his Sermons on Divine Unity. The seriousness of this charge, in the words of the historian Peter Adamson, "cannot be underestimated in the larger Muslim culture."
  
  Not long afterwards, Avicenna shifted his allegiance to the rising Buyid amir Shams al-Dawla, the younger brother of Majd al-Dawla, which Adamson suggests was due to Abu'l-Qasim also working under Sayyida Shirin. Avicenna had been called upon by Shams al-Dawla to treat him, but after the latter's campaign in the same year against his former ally, the Annazid ruler Abu Shawk (r. 1010–1046), he forced Avicenna to become his vizier.
  
  Although Avicenna would sometimes clash with Shams al-Dawla's troops, he remained vizier until the latter died of colic in 1021. Avicenna was asked to stay as vizier by Shams al-Dawla's son and successor Sama' al-Dawla (r. 1021–1023), but he instead went into hiding with his patron, Abu Ghalib al-Attar, to wait for better opportunities to emerge. It was during this period that Avicenna was secretly in contact with Ala al-Dawla Muhammad (r. 1008–1041), the Kakuyid ruler of Isfahan and uncle of Sayyida Shirin.
  
  It was during his stay at Attar's home that Avicenna completed The Book of Healing, writing 50 pages a day. The Buyid court in Hamadan, particularly the Kurdish vizier Taj al-Mulk, suspected Avicenna of correspondence with Ala al-Dawla, and as a result, had the house of Attar ransacked and ibn Sina imprisoned in the fortress of Fardajan, outside Hamadan. Juzjani blames one of ibn Sina's informers for his capture. He was imprisoned for four months until Ala al-Dawla captured Hamadan, ending Sama al-Dawla's reign.