Q:

TikZ logic alignment

I'm trying to create various logic circuit diagrams using the circuit library of TikZ (it allows multi-input gates).
I'm using a matrix, but this causes issues with the input wires not lining up with the input nodes (A and B). Also, the wires in general don't line up as nicely as they should do.
There must be a better way of doing this without using matrix, but I don't know what it would be.
Code:
\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{circuits.logic.US}

\begin{document}
\begin{tikzpicture}[circuit logic US, huge circuit symbols]
  \matrix[column sep=10mm]
    {
      \node (a) {A}; & \node [nand gate] (nand) {};        &                                        & \\
      \node (b) {B}; &                                     &                                        & \\
      \node (c) {C}; &                                     & \node [nor gate, inputs=nnn] (nor) {}; & \node [or gate, inputs=nnn] (out) {}; \\
                     & \node[not gate,rotate=90] (not) {}; &                                        & \\
      \node (d) {D}; &                                     &                                        & \\
    };
    \draw (a.east) -- ++(right:5mm)       |- (nand.input 1);
    \draw (b.east) -- ++(right:5mm)       |- (nand.input 2);
    \draw (c.east) -- ++(right:5mm)       |- (nor.input 2);
    \draw (d.east) -- ++(right:52mm)      |- (out.input 3);
    \draw (d.east)                        -| (not.input);
    \draw (not.output)                    |- (nor.input 3);
    \draw (nand.output) -- ++(right:30mm) |- (out.input 1);
    \draw (nand.output) -- ++(right:5mm)  |- (nor.input 1);
    \draw (nor.output) -- ++(right:5mm)   |- (out.input 2);
    \draw (out.output) -- ++(right:10mm);
\end{tikzpicture}
\end{document}

A:

If you place input nodes inside the matrix they will be placed as any other cell, so your B node defines a new row (the second one), while nand gate is still on first row.
One possibility to avoid this problem is to use \matrix to place only the gates and after that place your input nodes where you want. To place the nodes you can use every input and output of your already defined circuit.
To obtain next figure

I've started placing node C with  
\node (c) at ([xshift=-10mm]nand.west|-nor.input 2) {C};

and using it to place nodes A, B and D. In this case I placed them after "backdrawing" from circuit input to node place 
 \draw (nand.input 2) -- (nand.input 2-|c.east) node[left] (b) {B};

Same happen with connections between output inputs 1 and 3 with nand output or not input.
The complete code is:
\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{circuits.logic.US}

\begin{document}
\begin{tikzpicture}[circuit logic US, huge circuit symbols]
  \matrix[column sep=10mm]
    {
    \node [nand gate] (nand) {}; &  & \\
        & \node [nor gate, inputs=nnn] (nor) {}; & 
          \node [or gate, inputs=nnn] (out) {}; \\
    \node[not gate,rotate=90] (not) {}; &  & \\
    };

 \node (c) at ([xshift=-10mm]nand.west|-nor.input 2) {C};
 \draw (nor.input 2) -- (c);
 \draw (nand.input 2) -- (nand.input 2-|c.east) node[left] (b) {B};
 \draw (nand.input 1) -- (nand.input 1-|c.east) node[left] (a) {A};
 \draw (not.input) --++(270:3mm) coordinate (Daux) --(Daux-|c.east) node[left] (d) {D};

 \draw (not.output) |- (nor.input 3);
 \draw (out.input 3) --++(180:5mm) |- (Daux);
 \draw (out.input 1) --++(180:5mm) |- (nand.output);
 \draw (nor.input 1) --++(180:5mm) |- (nand.output);
 \draw (nor.output) -- ++(right:5mm)   |- (out.input 2);
 \draw (out.output) -- ++(right:10mm);
\end{tikzpicture}
\end{document}

