Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Control Structures

by mstone (Deacon)
on May 10, 2005 at 19:38 UTC ( [id://455728]=note: print w/replies, xml ) Need Help??


in reply to Control Structures

Well, speaking with tongue partially in cheek, I suppose Fredekin gates and generalized Toffoli gates would be nice. The code versions of each look roughly like so:

sub fredekin { my ($a,$b,$c) = @_; return (($a) ? ($a,$c,$b) : ($a,$b,$c)); } sub toffoli { my @list = @_; my $tail = pop @list; my $state = 0; for my $i (@list) { $state++ if ($i); } if ($state == @list) { $tail = ($tail) ? 0 : 1; } return ((@list, $tail)); }

The Fredekin gate takes a three-item list as input and returns a three-item list as output. If the first item is FALSE, the return list is identical to the input list. If the first item is TRUE, the last two items of the return list are swapped.

The Toffoli gate is more general, and does roughly the opposite. It takes an N-item list as input, and returns an N-item list as output. If any item from 0 to N-1 is FALSE, the output list is indentical to the input list. If all the items from 0 to N-1 are TRUE, the logic value of the final item is flipped.. TRUE is replaced by FALSE, or FALSE is replaced by TRUE.

Both gates are universal, meaning you can build a complete Turing machine using nothing but arrays of either kind of gate. The Fredekin gate also maintains perfect energy balance, meaning it never changes the number of TRUE or FALSE statements. The Toffoli gate can change the number of TRUE and FALSE statements, which makes it slightly more powerful than the Fredekin gate (i.e.: you'll need fewer gates and scratch inputs to solve a problem with Toffoli gates), but as a consequence, the Toffoli gate runs 'hotter'. Changing in the number of TRUE and FALSE statements involves energy transfer, and ultimately, that energy will be released as heat.

Both can serve as basic building blocks for quantum and/or reversible computation.

And as an aside, the Fredekin gate is actually kind of useful in everyday programming. It's good for situations where you need to choose between two options based on the value of a third item. And situations like that show up frequently when you try to arrange code for logical correctness.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://455728]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-20 03:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found