Image copyright GCHQ Image caption Historically gay people were not allowed to join GCHQ, which has its headquarters in Cheltenham

Not letting gay people work for GCHQ until the 1990s was wrong and was the "nation's loss", the UK intelligence agency's boss Robert Hannigan has said.

He also said the treatment of gay computer pioneer and code breaker Alan Turing had been "horrifying".

GCHQ now relies on people who "dare to be different", he told a conference hosted by gay rights group Stonewall.

Mr Hannigan said no-one could know what some of those sacked for being gay would have gone on to achieve.

Common practice

In his speech in London, Mr Hannigan said a former spy he called Ian, who was forced out of the service on suspicion of being gay in the 1960s, had urged him to apologise.

"I am happy to do so today and to say how sorry I am that he and so many others were treated in this way, right up until the 1990s when the policy was rightly changed," he said.

"The fact that it was common practice for decades reflected the intolerance of the times and the pressures of the Cold War, but it does not make it any less wrong and we should apologise for it.

"Their suffering was our loss and it was the nation's loss too because we cannot know what Ian and others who were dismissed would have gone on to do and achieve. We did not learn our lesson from Turing."

Despite his pivotal and groundbreaking work breaking codes during World War Two - the story of which was told in the 2014 film The Imitation Game staring Benedict Cumberbatch - Turing was hounded by GCHQ over his sexuality.

Image copyright Getty Images Image caption Mr Turing - pictured here aged 16 in 1928 - died after eating an apple laced with cyanide

In 1952 he faced a conviction for gross indecency following which he was chemically castrated. He had been arrested after having an affair with a 19-year-old Manchester man.

The conviction meant he lost his security clearance and had to stop the code-cracking work.

In 1954 he took his own life by eating an apple laced with cyanide, and in 2013 he received a posthumous royal pardon.

"In the horrifying story of his treatment, a small ray of light is that he was not abandoned by all of his colleagues at GCHQ - many stood by him," Mr Hannigan said.

He said GCHQ now relies on people who "dare to think differently and be different" and that included hiring spies on the autistic spectrum, with Asperger's or other syndromes, who he described as "precious assets".