# How expensive is it to verify that a gate is universal?

I know that a NAND gate is considered universal because we can use it to implement an AND, OR, and NOT gate.

Given an arbitrary gate, or set of gates, how many operations would it take to verify if these gates are universal (by brute force), and does there exist a more efficient way to do this? How expensive is this in general?

With the example of the NAND gate we can construct a NOT, AND, and OR, pretty easily, but is there a systematic way to do this?

More specifically, assume we are given $$n$$ two input, one output gates, where the gates are specified as a truth table.

Is there a way to also generalize this to, given $$n$$ gates, in which all the gates take $$m$$ inputs and 1 output, how expensive would this be?

• Can you clarify how exactly n works? Is it the quantity of gates of a given type (e.g. you have three NAND gates, just enough to make an OR gate) or is it the number of different types of gates in a collection, each of which is available in unlimited quantities? – Blckknght Oct 3 '20 at 17:54
• @Jlee523 are constants allowed? For example could I take one of your $n$ to $1$ gates and fill all inputs except one with constant ones and zeros? – plshelp Oct 3 '20 at 19:45

The problem is actually much more tractable than the other answers suggest. Specifically, for your first problem

assume we are given 𝑛 two input, one output gates, where the gates are specified as a truth table.

I give an $$O(n)$$ solution. The problem is more difficult in the generalization to where each gate has $$m$$ inputs, where the solution below will be $$O(n \cdot m \cdot 2^m)$$. But this is in principle kind of unavoidable, because it actually takes $$n \cdot 2^m$$ just to write out all the truth tables. In other words, if you don't spend this amount of time, you won't even be able to test all possible inputs to your existing gates, so it makes sense you wouldn't be able to determine universality. It's possible the additional $$O(m)$$ factor can be avoided, though it's not very significant compared to the $$2^m$$.

I will give a solution that:

1. Either proves universality or disproves universality, in a finite amount of time; and
2. Works for any number of gates.

The solution actually falls out of a very beautiful and lesser-known theorem of universal algebra due to Emil Post in 1941. Your problem is, given a set of gates, to determine if they are Functionally Complete. In what follows, we assume that constants are not allowed; but if you prefer to allow the constants $$1$$ and $$0$$, then just add constant gates (with 0 inputs) that output $$1$$ and $$0$$, respectively.

Theorem (Post, 1941). A set of gates is NOT functionally complete if and only if one of the following five conditions is satisfied:

• All of the gates are monotone. This means that if any of the inputs change from false to true, the output will only change from false to true and never from true to false (if it changes at all). For example, AND and OR are monotone; NAND, NOR, and NOT are not.

• All of the gates are affine. This means that the output depends in a "linear" way on the input; it means that the gate is really just an XOR of some of the input variables. NOT is also affine.

• All of the gates are self-dual. This means that switching false and true in all inputs always switches false and true in the output. For example, NOT is self-dual as is the MAJORITY gate, but AND, XOR, and OR aren't. Note also that if you allow constant inputs (1 and 0), then these are not self-dual and so this criterion is not satisfied.

• All of the gates are truth-preserving. This means that if all inputs are true, the output is true. For example, the constant 1 is truth-preserving, as is AND, but 0 is not.

• All of the gates are false-preserving. This means if all inputs are false, the output is false.

We can use this theorem to derive the following $$O(n \cdot m \cdot 2^m)$$ decision procedure for deciding whether $$n$$ gates, with $$m$$ inputs each, are expressively complete.

• First run through all the gates, and check if they are truth-preserving or false-preserving. This can be done in $$O(1)$$ per gate because we only have to check two inputs (all false, and all true). If all gates are truth-preserving or all are false-preserving, return false.

• Next run through all gates and check if they are self-dual. This takes $$O(2^m)$$ per gate because we need to check, for each input, how it compares to the negated input where true and false are switched. If all gates are self-dual, return false.

• Next we run through all gates and check if they are monotone. This requires checking for each of the $$2^m$$ possible inputs, what happens if any of the $$m$$ input wires switch from false to true, so it takes $$m \cdot 2^m$$. If all gates are monotone, return false.

• Finally, we run through all gates to check if they are affine. Iterate through each of the $$m$$ input wires. For each wire, then try all possibilities for all the other $$(m-1)$$ wires, with that wire both true and false. If the gate is affine, you should see that irrespective of the other wires, either this wire always has no effect on the output, or it always flips the output. If neither of these behaviors is observed for some input wire then the gate is not affine. If all gates are found to be affine, return false.

Otherwise, once all of these checks pass (there's at least one non-truth-preserving gate, at least one non-false-preserving gate, at least one non-monotone gate, at least one non-self-dual gate, and at least one non-affine gate), then return True.

Note: If you are allowing constant inputs 1 and 0, then modify the algorithm by removing the "self-dual", "truth-preserving", and "false-preserving" checks. So, all you have to do is check if all your gates are monotone and if all your gates are affine.

Finally, here are a couple of examples to illustrate the way the theorem works. First, for the NAND gate: NAND is special because it is not false-preserving, not truth-preserving, not monotone, not self-dual, and not affine. As a result, it alone is functionally complete. Another common example of a functionally complete set of gates is NOT and AND. In this case: NOT is not truth-preserving, false-preserving, and monotone, covering those three requirements; and AND is not self-dual or affine, covering the other two requirements. But if either AND or NOT is dropped, then not all five requirements are satisfied.

• The last three of your conditions are no longer sufficient if constant inputs are allowed. NAND(a, b) = NOT(M3(a, b, 1)) – John Dvorak Oct 4 '20 at 1:41
• @JohnDvorak Thanks for the correction, I will clarify. With constant inputs allowed, the "self-dual" condition goes away because constants are not self-dual. In the theorem, constants are thought of as gates with 0 inputs and 1 output. With that in mind, the theorem still goes through. – 6005 Oct 4 '20 at 1:48
• Great answer! Lovely. – D.W. Oct 4 '20 at 3:31

A gate with m input bits has $$2^m$$ possible inputs. Each possible input has 2 possible output values, so there are $$2^{2^m}$$ possible such gates. m = 2 -> 16 gates, m = 3 -> 256 gates, m = 4 -> 65,536 gates, m= 5 -> about 4 billion gates, m = 6 -> about 18 billion billion gates.

You can easily write a program that does an exhaustive search to check if all the gates can be produced. That will run very quickly with m <= 4, will take a lot of patience for m = 5, and doesn’t have a chance in hell with m = 6.

However, if you do this, then in practice there is a good chance that either you find a NAND gate quickly, or that you quickly run out of further gates.

• Why should I have to check all $2^{2^m}$ possible gates to determine if all gates can be produced? I only have to show that NAND can be produced, not all. In fact, there is a solution that works in $O(n \cdot m \cdot 2^m)$ for $n$ gates with $m$ inputs each, see my answer. – 6005 Oct 4 '20 at 1:41