Optional Arguments for Macros Called within Tikz Matrix

You could use xparse and \NewExpandableDocumentCommand, but the \optmeter command should have a dummy mandatory argument.

\documentclass{article}
\usepackage{tikz-cd}
\usepackage{xparse}

\newcommand{\qw}{\arrow[dash,thick]{l}}

%hardcoded parameters
\newcommand{\hardmeter}[0]{|[operator,label={[yshift=0.1cm]above right:0}]| {} \qw}
\newcommand{\hardgate}[1]{|[operator,fill=blue]| #1 \qw}
%optional parameters
\NewExpandableDocumentCommand{\optmeter}{O{a}m}{%
  |[operator,label={[yshift=0.1cm]above right:#1}]| {} \qw
}
\NewExpandableDocumentCommand{\optgate}{O{a}{m}}{|[operator,#1]| #2 \qw}

\tikzcdset{
  nodes in empty cells,
  every matrix/.append style={name=m},
} %fix name of matrix as m
\tikzset{
  every node/.style={
    anchor=center,
    minimum size=0pt,
    inner sep=0pt,
    outer sep=0pt,
    thick,
  },
  operator/.style={
    draw,
    fill=white,
    minimum size=1.5em,
  },
}

\begin{document}
What's supposed to happen:
\[
\begin{tikzcd}
& \hardgate{H} & \hardmeter
\end{tikzcd}
\]
What actually happens:
\[
\begin{tikzcd}
& \optgate[fill=blue]{H} & \optmeter[0]{}
\end{tikzcd}
\]
\end{document}

enter image description here


Just for completeness. TikZ allows you to do most things just with styles. That is, I am not sure I would use a command for that.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{cd}
\newcommand{\qw}{\arrow[dash,thick]{l}}
%hardcoded parameters
\newcommand{\hardmeter}[0]{|[operator,label={[yshift=0.1cm]above right:0}]| {} \qw}
\newcommand{\hardgate}[1]{|[operator,fill=blue]| #1 \qw}

\tikzcdset{nodes in empty cells,every matrix/.append style={name=m}} %fix name of matrix as m
\tikzset{
    every node/.style={
        anchor=center,minimum size=0pt,inner sep=0pt,outer sep=0pt,thick
    },
    operator/.style={draw,fill=white,minimum size=1.5em},
    hard/.style={operator,label={[yshift=0.1cm]above right:#1},fill=blue,
    append after command={\pgfextra{\arrow[dash,thick]{l}}}}
}

\begin{document}
What's supposed to happen: \\\\
\begin{tikzcd}
& \hardgate{H} & \hardmeter
\end{tikzcd}\\\\
What actually happens:\\
\begin{tikzcd}
& |[hard]| H & |[hard=0]| O & |[hard,fill=red]| U & |[hard=2,fill=red]| R
\end{tikzcd}
\end{document} 

enter image description here

UPDATE: Sort of an answer to your updated question. I patched something that does what I think it is supposed to do. It is IMHO a very bizarre construction. The basic trick is to defer the ifthenelse to /quant/wires/, which had to become a code, and then to use \pgfkeysalso to smuggle the options back to the node style.

\documentclass{article}
\usepackage{tikz,xparse,ifthen}
\usetikzlibrary{cd}
\NewExpandableDocumentCommand{\meter}{O{}m}{%
|[#1,label=#2]| {}
}
\tikzset{
    every node/.style={
        anchor=center,minimum size=0pt,inner sep=0pt,outer sep=0pt,thick
    },
    operator/.style={draw,fill=white,minimum size=1.5em}
}
\pgfkeys{/quant/wires/.code={\ifcase#1
\or
\pgfkeysalso{/tikz/.cd,operator}
\or
\pgfkeysalso{/tikz/.cd,operator,fill=blue}
\or
\fi}}
\begin{document}
\begin{center}
\begin{tikzcd}
 \meter{0} & \meter[/quant/wires=1]{0} & \meter[/quant/wires=2]{0}
\end{tikzcd}
\end{center}
\end{document} 

enter image description here

I understand that this will kill all your remaining sympathy for marmots, but I do not think that it is a good style to unaccept a working answer just because it does not meet requirements that were added afterwards. I do believe that @egreg deserves the check mark next to his nice answer, which I am using here, and that you should ask a new question here. After all, questions are free of charge.