# Qcircuit: color background in gategroup

I'm looking for a way to use color in qcircuit (based on xy-pic). I'd like to be able to add a background color for a gate, and for a gategroup. For exemple, how can I change this code:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{graphicx}
\usepackage[]{qcircuit}

\begin{document}
\Qcircuit @C=1em @R=1em {
& \ctrl{2} & \qw & \gate{H} & \ctrl{1} &
\gate{H} & \qw \\
& \qw & \ctrl{1} & \gate{H} & \targ &
\gate{H} & \qw \\
& \targ & \targ & \gate{Z} & \qw & \ctrl{-1} &
\qw \gategroup{1}{4}{2}{6}{.7em}{-}
}
\end{document}


so that it produces something like that:

Thank you !

• – John Kormylo Sep 3 '17 at 13:56
• @JohnKormylo: I would like to avoid to draw circuits in tikz, because for now it takes to much time, and it's not easy to draw nodes that span several wires in a clear way... – tobiasBora Sep 3 '17 at 14:45

There is no convenient way to do this using Qcircuit and Xy-pic. I generally use the pgf package to draw colored boxes behind the regions of the circuit of interest. Aligning the colors precisely with the circuit elements is a pain, but it can be done as shown below:

This was typeset using the following code:

\documentclass{standalone}
\usepackage{color}
\usepackage{pgf}
\input{Qcircuit}% using  Qcircuit version 2.5
\begin{document}
\hspace{1em}
\begin{tabular}{c}
\vspace{-.6em}\\
\begin{pgfpicture}{0em}{0em}{0em}{0em}
\color{cyan}
\pgfrect[fill]{\pgfpoint{5.05em}{-5.2em}}{\pgfpoint{1.35em}{1.25em}}
\color{green}
\pgfrect[fill]{\pgfpoint{4.6em}{-3.28em}}{\pgfpoint{6.55em}{4.3em}}
\end{pgfpicture}
\Qcircuit @C=1em @R=1em {
& \ctrl{2} & \qw & \gate{H} & \ctrl{1} & \gate{H} & \qw \\
& \qw & \ctrl{1} & \gate{H} & \targ & \gate{H} & \qw \\
& \targ & \targ & \gate{Z} & \qw & \ctrl{-1} & \qw
\gategroup{1}{4}{2}{6}{.7em}{-}
}
\vspace{1.2em}\hspace{1.2em}
\\
\end{tabular}
\end{document}