Towards a Design Flow for Reversible Logic
Robert Wille Rolf Drechsler
Towards a Design Flow for Reversible Logic
Robert Wille Institute of Computer Science University of Bremen Bibliothekstr. 1 28359 Bremen Germany
[email protected]
Rolf Drechsler Institute of Computer Science University of Bremen Bibliothekstr. 1 28359 Bremen Germany
[email protected]
ISBN 978-90-481-9578-7 e-ISBN 978-90-481-9579-4 DOI 10.1007/978-90-481-9579-4 Springer Dordrecht Heidelberg London New York Library of Congress Control Number: 2010932404 © Springer Science+Business Media B.V. 2010 No part of this work may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, microfilming, recording or otherwise, without written permission from the Publisher, with the exception of any material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work. Cover design: eStudio Calamar S.L. Printed on acid-free paper Springer is part of Springer Science+Business Media (www.springer.com)
Preface
The development of computing machines found great success in the last decades. But the ongoing miniaturization of integrated circuits will reach its limits in the near future. Shrinking transistor sizes and power dissipation are the major barriers in the development of smaller and more powerful circuits. Reversible logic provides an alternative that may overcome many of these problems in the future. For low-power design, reversible logic offers significant advantages since zero power dissipation will only be possible if computation is reversible. Furthermore, quantum computation profits from enhancements in this area, because every quantum circuit is inherently reversible and thus requires reversible descriptions. However, since reversible logic is subject to certain restrictions (e.g. fanout and feedback are not directly allowed), the design of reversible circuits significantly differs from the design of traditional circuits. Nearly all steps in the design flow (like synthesis, verification, or debugging) must be redeveloped so that they become applicable to reversible circuits as well. But research in reversible logic is still at the beginning. No continuous design flow exists so far. In this book, contributions to a design flow for reversible logic are presented. This includes advanced methods for synthesis, optimization, verification, and debugging. Formal methods like Boolean satisfiability and decision diagrams are thereby exploited. By combining the techniques proposed in the book, it is possible to synthesize reversible circuits representing large functions. Optimization approaches ensure that the resulting circuits are of small cost. Finally, a method for equivalence checking and automatic debugging allows to verify the obtained results and helps to accelerate the search for bugs in case of errors in the design. Combining the respective approaches, a first design flow for reversible circuits of significant size results. This book addresses computer scientists and computer architects and does not require previous knowledge about the physics of reversible logic or quantum computation. The respective concepts as well as the used models are briefly introduced.
v
vi
Preface
All approaches are described in a self-contained manner. The content of the book does not only conveys a coherent overview about current research results, but also builds the basis for future work on a design flow for reversible logic. Bremen
Robert Wille Rolf Drechsler
Acknowledgements
This book is the result of more than three years of intensive research in the area of reversible logic. During this time, we experienced many support from different people for which we would like to thank them very much. In particular, the Group of Computer Architecture at the University of Bremen earns many thanks for providing a comfortable and inspirational environment. Many thanks go to Stefan Frehse, Daniel Große, Lisa Jungmann, Hoang M. Le, Sebastian Offermann, and Mathias Soeken who actively helped in the development of the approaches described in this book. Sincere thanks also go to Prof. D. Michael Miller from the University of Victoria, Prof. Gerhard W. Dueck from the University of New Brunswick, and Dr. Mehdi Saeedi from the Amirkabir University of Technology in Tehran for very fruitful collaborations. In this context, we would like to thank the German Academic Exchange Service (DAAD) which enabled the close contact with the groups in Canada. Special thanks go to the German Research Foundation (DFG) which funded parts of this work under the contract number DR 287/20-1. Finally, we would like to thank Marc Messing who did a great job of proofreading as well as Christiane and Shawn Mitchell who closely checked the manuscript for English style and grammar.
vii
Contents
1
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2
Preliminaries . . . . . . . . . . . . . . . . . . . . . . . . 2.1 Background . . . . . . . . . . . . . . . . . . . . . . 2.1.1 Reversible Functions . . . . . . . . . . . . . . 2.1.2 Reversible Circuits . . . . . . . . . . . . . . . 2.1.3 Quantum Circuits . . . . . . . . . . . . . . . 2.2 Decision Diagrams . . . . . . . . . . . . . . . . . . . 2.2.1 Binary Decision Diagrams . . . . . . . . . . . 2.2.2 Quantum Multiple-valued Decision Diagrams 2.3 Satisfiability Solvers . . . . . . . . . . . . . . . . . . 2.3.1 Boolean Satisfiability . . . . . . . . . . . . . 2.3.2 Extended SAT Solvers . . . . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
7 7 7 9 13 16 17 19 21 21 22
3
Synthesis of Reversible Logic . . . . . . . . . 3.1 Current Synthesis Steps . . . . . . . . . . 3.1.1 Embedding Irreversible Functions . 3.1.2 Transformation-based Synthesis . . 3.2 BDD-based Synthesis . . . . . . . . . . . 3.2.1 General Idea . . . . . . . . . . . . 3.2.2 Exploiting BDD Optimization . . . 3.2.3 Theoretical Consideration . . . . . 3.2.4 Experimental Results . . . . . . . 3.3 SyReC: A Reversible Hardware Language 3.3.1 The SyReC Language . . . . . . . 3.3.2 Synthesis of the Circuits . . . . . . 3.3.3 Experimental Results . . . . . . . 3.4 Summary and Future Work . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
27 28 28 30 31 32 34 37 39 46 47 50 53 56
4
Exact Synthesis of Reversible Logic . . . . . . . . . . . . . . . . . . . 4.1 Main Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.2 SAT-based Exact Synthesis . . . . . . . . . . . . . . . . . . . . .
57 58 61
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
1
ix
x
Contents
4.2.1 Encoding for Toffoli Circuits . . . . . . 4.2.2 Encoding for Quantum Circuits . . . . . 4.2.3 Handling Irreversible Functions . . . . . 4.2.4 Experimental Results . . . . . . . . . . 4.3 Improved Exact Synthesis . . . . . . . . . . . . 4.3.1 Exploiting Higher Levels of Abstractions 4.3.2 Quantified Exact Synthesis . . . . . . . 4.3.3 Experimental Results . . . . . . . . . . 4.4 Summary and Future Work . . . . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
61 65 68 70 76 77 81 84 91
5
Embedding of Irreversible Functions . 5.1 The Embedding Problem . . . . . 5.2 Don’t Care Assignment . . . . . . 5.2.1 Methods . . . . . . . . . . 5.2.2 Experimental Results . . . 5.3 Synthesis with Output Permutation 5.3.1 General Idea . . . . . . . . 5.3.2 Exact Approach . . . . . . 5.3.3 Heuristic Approach . . . . 5.3.4 Experimental Results . . . 5.4 Summary and Future Work . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
93 94 96 96 99 100 102 104 105 106 111
6
Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.1 Adding Lines to Reduce Circuit Cost . . . . . . . . . . . . . . 6.1.1 General Idea . . . . . . . . . . . . . . . . . . . . . . . 6.1.2 Algorithm . . . . . . . . . . . . . . . . . . . . . . . . 6.1.3 Experimental Results . . . . . . . . . . . . . . . . . . 6.2 Reducing the Number of Circuit Lines . . . . . . . . . . . . . 6.2.1 General Idea . . . . . . . . . . . . . . . . . . . . . . . 6.2.2 Algorithm . . . . . . . . . . . . . . . . . . . . . . . . 6.2.3 Experimental Results . . . . . . . . . . . . . . . . . . 6.3 Optimizing Circuits for Linear Nearest Neighbor Architectures 6.3.1 NNC-optimal Decomposition . . . . . . . . . . . . . . 6.3.2 Optimizing NNC-optimal Decomposition . . . . . . . . 6.3.3 Experimental Results . . . . . . . . . . . . . . . . . . 6.4 Summary and Future Work . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
113 114 114 116 117 124 125 127 130 131 133 134 138 138
7
Formal Verification and Debugging . . . . . . . 7.1 Equivalence Checking . . . . . . . . . . . . 7.1.1 The Equivalence Checking Problem . 7.1.2 QMDD-based Equivalence Checking 7.1.3 SAT-based Equivalence Checking . . 7.1.4 Experimental Results . . . . . . . . 7.2 Automated Debugging and Fixing . . . . . . 7.2.1 The Debugging Problem . . . . . . . 7.2.2 Determining Error Candidates . . . .
. . . . . . . . .
. . . . . . . . .
143 144 145 145 148 150 154 155 157
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . .
. . . . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
Contents
7.2.3 Determining Error Locations 7.2.4 Fixing Erroneous Circuits . . 7.2.5 Experimental Results . . . . 7.3 Summary and Future Work . . . . . 8
xi
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
161 165 167 173
Summary and Conclusions . . . . . . . . . . . . . . . . . . . . . . . 175
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
Acronyms
BDDs CMOS CNF CNOT d HDL LNN NNC MCF MCT P QMDD QF_BV QBF SAT SMT SWOP
Binary Decision Diagrams Complementary Metal Oxide Semiconductor Conjunctive Normal Form Controlled-NOT Number of gates (depth) of a circuit Hardware Description Language Linear Nearest Neighbor Nearest Neighbor Cost Multiple control Fredkin Multiple control Toffoli Peres Quantum Multiple-valued Decision Diagram Quantifier free bit-vector logic Quantified Boolean Formulas Boolean satisfiability SAT Modulo Theories Synthesis with Output Permutation
xiii
Chapter 1
Introduction
In the last decades, great achievements have been made in the development of computing machines. While computers consisting of a few thousands of components filled whole rooms in the early 70’s, nowadays billions of transistors are built on some square millimeters. This is a result of the achievements made in the domain of semiconductors which are still holding on: The number of transistors in a circuit doubles every 18 months (which is also known as Moore’s Law according to the founder of Intel, Gordon E. Moore, who formulated this as a prediction in 1965 [Moo65]).1 Until today, this prediction has not lost any of its validity—each year more complex systems and chips are introduced. However, it is obvious that such an exponential growth must reach its limits in the future—at least when the miniaturization reaches a level, where single transistor sizes are approaching the atomic scale. Besides that, power dissipation more and more becomes a crucial issue for designing high performance digital circuits. In the last decades, the amount of power dissipated in the form of heat to the surrounding environment of a chip increased by orders of magnitudes. Since excessive heat may decrease the reliability of a chip (or even destroys it), power dissipation is one of the major barriers to progress the development of smaller and faster computer chips. Due to these reasons, some researchers expect that from the 2020s on, duplication of transistor density will not be possible any longer. To further satisfy the needs for more computational power, alternatives are needed that go beyond the scope of “traditional” technologies like CMOS.2 Reversible logic marks a promising new direction where all operations are performed in an invertible manner. That is, in contrast to traditional logic, all computations can be reverted (i.e. the inputs can be obtained from the outputs and vice versa). A simple standard operation like the logical AND already illustrates that reversibility is not guaranteed in traditional circuit systems. Indeed, it is possible to obtain the inputs of an AND gate if the output is assigned to 1 (then both inputs must be as1 Originally,
Moore predicted a doubling every 12 months; ten years later he updated to 18 months.
2 CMOS
is the abbreviation for Complementary Metal Oxide Semiconductor, the technology mainly used for today’s integrated circuits. R. Wille, R. Drechsler, Towards a Design Flow for Reversible Logic, DOI 10.1007/978-90-481-9579-4_1, © Springer Science+Business Media B.V. 2010
1
2
1 Introduction
signed to 1 as well). But, it is not possible to determine the input values if the AND outputs 0. In contrast, reversible logic allows bijective operations only, i.e. n-input n-output functions that map each possible input vector to a unique output vector. This reversibility builds the basis for emerging technologies that may replace or at least enhance the traditional computer chip. Two examples of such technologies making use of reversible logic are sketched in the following: • Reversible Logic for Low-Power Design As mentioned above, power dissipation and therewith heat generation is a serious problem for today’s computer chips. A significant part of energy dissipation is due to the non-ideal behaviors of transistors and materials. Here, higher levels of integration and new fabrication processes reduced the heat generation in the last decade. However, a more fundamental reason for power dissipation arises from the observations made by Landauer in 1961 [Lan61]. Landauer proved that using traditional (irreversible) logic, gates always lead to energy dissipation regardless of the underlying technology. More precisely, exactly k · T · log 2 Joule of energy is dissipated for each “lost” bit of information during the irreversible operation (where k is the Boltzmann constant and T is the temperature). While this amount of power currently does not sound significant, it may become crucial additionally considering that (1) today millions of operations are performed in some seconds (i.e. increasing processor frequency multiplies this amount) and (2) more and more operations are performed with smaller and smaller transistor sizes (i.e. in a smaller area). In contrast, Bennett showed that energy dissipation is reduced or even eliminated if computation becomes information-lossless [Ben73]. This holds for reversible logic, since data is bijectively transformed without losing any of the original information. Bennett proved that circuits with zero power dissipation are only possible if they are built from reversible gates. In 2002, first reversible circuits have been built that exploit this observation [DV02]. In fact, these circuits were powered by their input signals only (i.e. without additional power supplies). In the future, such circuits may be an alternative that can cope with the heat generation problem of traditional chips. Furthermore, since reversible circuits already work with low power, applications are also possible in domains where power is a limited resource (e.g. for mobile computation). • Reversible Logic as a Basis for Quantum Computation Quantum circuits [NC00] offer a new kind of computation. Instead of logic signals 0 and 1, quantum circuits make use of qubits. A qubit is a two level quantum system, described by a two dimensional complex Hilbert space. The resulting tools can be obtained under www.revkit.org. This allows to represent not only 0 and 1 but also a superposition of both. As a result, qubits may represent multiple states at the same time enabling enormous speed-ups in computations. For example, it has been shown that using a quantum circuit it is possible to solve the factorization problem in polynomial time, while for traditional circuits only exponential methods exist [Sho94, VSB+01].
1 Introduction
3
But, research in the area of quantum circuits is still at the beginning. Nevertheless, first promising results exist: At the University of Innsbruck one of the first quantum circuits consisting of 8 qubits was built in 2005. This has been further improved so that today circuits with dozens of qubits exists—with upward trend. Even first commercial realizations of quantum circuits (e.g. a random number generator) are available. Reversible logic is important in this area because every quantum operation is inherently reversible. Thus, progress in the domain of reversible logic can be directly applied to quantum logic. Besides that, reversible logic additionally finds application in domains like optical computing [CA87], DNA computing [TS05], as well as nanotechnologies [Mer93]. Also, cryptography or encoding/decoding methods (e.g. for music and videos) can profit from enhancements in this area (see e.g. [ML01]). Furthermore, already today reversible operations are used in instruction sets for microprocessors [SL00]. The basic concepts of reversible logic are thereby not new and have already been introduced in the 60’s by Landauer [Lan61] and were further refined by Bennett [Ben73] and Toffoli [Tof80]. They observed that due to the reversibility, a straightforward usage of fanouts and feedback is not possible in reversible logic. Furthermore, new libraries of (reversible) gates have been introduced to represent invertible operations [Tof80, FT82, Per85, NC00] and it was stated that each reversible circuit must be a cascade of these reversible gates. Even if this still represents the basis for research in the area of reversible logic, the topic was not intensively studied by computer scientists before the year 2000. The main reason for that may lie in the fact that applications of reversible logic (in particular in the domain of quantum computation) have been seen as “dreams of the future”. But, this changed as with factorization a very important problem (factorization builds the basis for most of the today’s encrypting methods) was solved on a physically implemented quantum circuit [Sho94, VSB+01]. Therewith, a proof of concept was available showing that quantum computing, in fact, may be one solution for future computational problems. In particular, this achievement (together with further ones e.g. in reversible CMOS design as mentioned above) significantly moved the topic forward so that nowadays reversible logic is seen as a promising research area. As a consequence, in the last years computer scientists have also started to develop new methods e.g. for synthesis of reversible circuits. However, no real design flow for reversible logic exists until today. This is crucial since, due to the mentioned restrictions (e.g. no fanout and feedback), the design of reversible circuits significantly differs from the design of traditional circuits. Nearly all elaborated methods for synthesis, verification, debugging, and test available for traditional circuit design must be redeveloped so that they become applicable to reversible circuits as well. Now, while applications of reversible logic are starting to become feasible and traditional technologies more and more suffer from the increasing miniaturization, it is even more necessary to work towards such a flow. Moreover, considering the traditional design flow, it can be concluded that until today, computer scientists cannot fully exploit the technical state-of-the-art. That is, the number of transistors that can be physically implemented on a chip grows faster
4
1 Introduction
Fig. 1.1 Proposed design flow
than the ability to design them in a useful manner (also known as the design gap). This becomes even more crucial if, additionally, the ability to verify the correctness of the designed circuits is considered (known as the verification gap). Once reversible logic becomes feasible for large designs in the future, researchers will be faced with similar challenges. Thus, it is worth working towards a design flow for reversible logic already today. First steps in this direction have been made in the domain of synthesis (see e.g. [SPMH03, MDM05]), verification (see e.g. [VMH07, GNP08]), and test (see e.g. [PHM04, PBL05, PFBH05]). However, they are all still far away from covering real design needs. As an example, most synthesis approaches are only applicable for small functions and often produce circuits with relatively high cost. In contrast, design methods to create complex circuits and to efficiently verify their correctness are needed. This book makes contributions to a future design flow for reversible logic by proposing advanced methods for synthesis, optimization, verification, and debugging. Figure 1.1 shows the interaction of the proposed steps in an integrated flow. The left-hand side sketches the restrictions or challenges, respectively, to be solved in comparison to traditional methods. By combining the techniques proposed in the book, it is possible to synthesize reversible circuits representing large functions. Optimization methods ensure that the resulting circuits are of small cost. Finally, methods for equivalence checking and automatic debugging allow to verify the obtained results and help to accelerate the search for bugs in case of errors in the design. In the following, the respective contributions are briefly introduced in the order they appear in this book. A more detailed description of the problems as well as the proposed solutions is given at the beginning of each chapter.
1 Introduction
5
As a starting point, synthesis is considered in Chap. 3. Currently, the synthesis of reversible logic or quantum circuits, respectively, is limited. In the past, only methods have been proposed that are applicable for relatively small functions, i.e. functions with at most 30 variables. In addition, these methods often require an enormous amount of run-time. After reviewing the reasons for these limitations, a new synthesis method based on Binary Decision Diagrams (BDDs) is proposed. This enables synthesis of functions containing over 100 variables and thus is a major step towards design of complex systems in reversible logic. Additionally, a hardware description language is introduced that allows to specify and afterwards synthesize complex circuits in reversible logic. The problem of exact synthesis is considered in Chap. 4. Exact synthesis methods generate minimal circuits for a given function. Naturally, exact synthesis approaches are only applicable for very small functions. But, the resulting circuit realizations can be used later e.g. as building blocks for heuristic approaches. Nevertheless, run-time is the limiting factor here. The chapter describes how techniques of Boolean satisfiability (SAT) can be exploited for efficient exact synthesis of reversible circuits. Further approaches incorporating problem-specific knowledge as well as quantification are then introduced. These methods allow further accelerations of the exact synthesis. With these different synthesis approaches as a basis, the problem of embedding is addressed in Chap. 5. Usually, most of the synthesis approaches require a reversible function as input. But, basic functions like AND or addition are inherently irreversible. Thus, before synthesis, these functions must be embedded into reversible ones. This requires the addition of extra circuit signals and therewith constant inputs, garbage outputs, as well as don’t care conditions at the outputs. Furthermore, the order of outputs may be chosen arbitrarily. All this affects the generated synthesis result. In Chap. 5, methods for finding good embeddings are proposed and evaluated. After synthesis, the resulting circuits often are of high cost. In particular, dedicated technology-specific constraints are not considered by synthesis approaches. To address this, in Chap. 6 three different optimization methods are introduced—each with an own focus on a particular cost metric. The first one considers the reduction of the well-established quantum cost (used in quantum circuits) and the transistor cost (used in CMOS implementations), respectively. The second one addresses the number of lines in a circuit which particularly is important for all quantum realizations. Finally, an approach is introduced that takes a new cost metric based on a dedicated physical realization of quantum circuits into account. This allows that designers can automatically optimize their circuits with respect to the special needs of the addressed technology. To ensure that the respective results (e.g. obtained by optimization) still represent the desired functionality, verification is applied. For this purpose, in Chap. 7 equivalence checkers are introduced that can handle circuits with several thousands of gates in a very short time. Furthermore, an automatic approach for debugging is proposed. Instead of manually searching for the source of an error, this method allows a fast calculation of a reduced set of error candidates to be considered or even to automatically fix the erroneous circuit, respectively.
6
1 Introduction
Fig. 1.2 Structure of the book
Altogether, the contributions of this book to the design flow for reversible logic can be summarized as follows: • Synthesis methods for large functions (i.e. functions with more than 100 variables) • A hardware description language for reversible logic • Exact approaches for synthesizing minimal circuits that can later be used as building blocks • Embedding methods to automatically realize circuits for irreversible functions • Optimization approaches to reduce the cost with respect to the addressed technology • Equivalence checking of large circuits (i.e. circuits with several thousands of gates) • Automatic debugging and fixing of erroneous circuits All proposed methods have been implemented and experimentally evaluated. To this end, a uniform format for specifying reversible functions as well as reversible circuits has been defined which was used in all experiments throughout this book (see also the note on benchmarks on p. 26). Furthermore, all benchmark functions as well as the circuits have been made online available at RevLib under www.revlib.org. The resulting tools can be obtained under www.revkit.org. This allows other researchers to compare their results with the ones obtained in this work. However, the results together with a discussion, related work, and future research directions are, of course, also given in the respective chapters. According to the outline sketched above, the remainder of this book is structured as depicted in Fig. 1.2. The next chapter gives a more detailed introduction into both reversible as well as quantum logic and provides the basic notations and definitions as used in the rest of this book. Afterwards, the chapters about synthesis (Chap. 3), optimization (Chap. 6), as well as verification and debugging (Chap. 7) can be read independently of each other. Only for Chap. 4 about exact synthesis and Chap. 5 about embedding irreversible functions is it recommended to read the previous chapters beforehand. Chapter 8 summarizes all findings and gives directions for future work.
Chapter 2
Preliminaries
This chapter provides the basic definitions and notations to keep the remaining book self-contained. The chapter is divided into three parts. In the first section, Boolean functions, reversible functions, and the respective circuit descriptions are introduced. This builds the basis for all approaches described in this book. Since many of the proposed techniques exploit decision diagrams and satisfiability solvers, respectively, the basic concepts of these core techniques are also introduced in the last two sections. All descriptions are thereby kept brief. For a more in-depth treatment, references to further reading are given in the respective sections.
2.1 Background Reversible logic realizes bijective Boolean functions. Thus, first the basics regarding Boolean functions are revisited and further extended by a description of the properties specifically applied to reversible functions. Then, reversible circuits as well as quantum circuits are introduced which are used as realizations of reversible functions.
2.1.1 Reversible Functions Every logic computation can be defined as a function over Boolean variables B ∈ {0, 1}. More precisely: Definition 2.1 A Boolean function is a mapping f : Bn → B with n ∈ N. A function f is defined over its input variables X = {x1 , x2 , . . . , xn } and hence is also denoted by f (x1 , x2 , . . . , xn ). The concrete mapping is described in terms of Boolean expressions which are formed over the variables from X and the operations ∧ (AND), ∨ (OR), as well as · (NOT). R. Wille, R. Drechsler, Towards a Design Flow for Reversible Logic, DOI 10.1007/978-90-481-9579-4_2, © Springer Science+Business Media B.V. 2010
7
8 Table 2.1 Boolean functions
2 Preliminaries (a) AND
(b) OR
(c) NOT
x1 x2
x1 ∧ x2
x1 x2
x1 ∨ x2
x1
x1
0 0
0
0 0
0
0
1
0 1
0
0 1
1
1
0
1 0
0
1 0
1
1 1
1
1 1
1
Example 2.1 Table 2.1 shows the truth tables of the operations AND, OR, and NOT, respectively. Each truth table has 2n rows, showing the mapping of each input pattern to the respective output pattern. Taking AND, OR, and NOT as a basis, every Boolean function can be derived. For example, the often used functions XOR, implication, and equivalence are derived as follows: • XOR: x1 ⊕ x2 := (x1 ∧ x 2 ) ∨ (x 1 ∧ x2 ) • Implication: x1 ⇒ x2 := x 1 ∨ x2 • Equivalence: x1 ⇔ x2 := x1 ⊕ x2 So far, single-output functions have been introduced. However, in practice also multi-output functions are widely used. Definition 2.2 A multi-output Boolean function is a mapping f : Bn → Bm with n, m ∈ N. More precisely, it is a system of Boolean functions fi (x1 , x2 , . . . , xn ) with 1 ≤ i ≤ m. In the following multi-output functions are also termed as n-input, m-output functions or n × m functions, respectively. Example 2.2 Table 2.2(a) shows the truth table of a 3-input, 2-output function representing the adder function. This book considers reversible functions. Reversible functions are a subset of multi-output functions and are defined as follows: Definition 2.3 A multi-output function f : Bn → Bm is a reversible function iff • its number of inputs is equal to the number of outputs (i.e. n = m) and • it maps each input pattern to a unique output pattern. In other words, each reversible function is a bijection that performs a permutation of the set of input patterns. A function that is not reversible is termed irreversible. Example 2.3 Table 2.2(c) shows a 3-input, 3-output function. This function is reversible, since each input pattern maps to a unique output pattern. In contrast the function depicted in Table 2.2(a) is irreversible, since n = m. Moreover, also the
2.1 Background
9
Table 2.2 Multi-output functions (a) Irrev. (Adder) (b) Irreversible
(c) Reversible
x1
x2
x3
f1
f2
x1 x2 x3
f1 f2 f3
x1 x2 x3
f1 f2 f3
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
1
0
0
1
0
0
0
0
0
1
0
1
0
0
1
0
0
1
0
1
0
0
1
0
0
1
0
1
0
0
0
1
1
1
0
0
1
1
0
1
1
0
1
1
1
0
1
1
0
0
0
1
1
0
0
1
0
0
1
0
0
0
0
1
1
0
1
1
0
1
0
1
1
0
1
1
0
1
0
1
1
1
1
0
1
0
1
1
0
1
1
1
1
1
0
1
1
0
1
1
1
1
1
1
1
1
1
1
0
1
1
1
1
1
1
function in Table 2.2(b) is irreversible. Here, the number n of inputs indeed is equal to the number m of outputs, but there is no unique input-output mapping. For example, both inputs 000 and 001 map to the output 000. Quite often, (irreversible) multi-output Boolean functions should be represented by reversible circuits. This necessitates the irreversible function to be embedded into a reversible one which requires the addition of constant inputs and garbage outputs defined as follows: Definition 2.4 A constant input of a reversible function is an input that is set to a fixed value (either 0 or 1). Definition 2.5 A garbage output of a reversible function is an output which is a don’t care for all possible input conditions. The problem of embedding is an integral part of synthesis which is described later in this book. In particular, Sect. 3.1.1 and Chap. 5 cover the respective aspects in detail. Reversible functions can be realized by reversible logic. Due to its special properties, reversible logic found large interest in several domains like low-power design or quantum computation (see Chap. 1). As a result, synthesis of reversible functions has become an intensively studied topic in the last years. Therefore, new kinds of circuits have been proposed that are introduced and compared to traditional circuits in the next section.
2.1.2 Reversible Circuits A circuit realizes a Boolean function. Usually, a circuit is composed of signal lines and a set of basic gates (called gate library). For traditional circuits, often the gate
10
2 Preliminaries
Fig. 2.1 Traditional circuit elements
library depicted in Fig. 2.1 is used. This includes gates for the operations AND, OR, and NOT, based on which any Boolean function can be realized. Furthermore, fanouts are applied to use signal values more than once. In contrast, to realize reversible logic some restrictions must be considered: fanouts and feedback are not directly allowed, since they would destroy the reversibility of the computation [NC00]. Also, the gate library from above as well as the traditional design flow cannot be utilized. As a result, a cascade structure over reversible gates is the established model to realize reversible logic. Definition 2.6 A reversible circuit G over inputs X = {x1 , x2 , . . . , xn } is a cascade of reversible gates gi , i.e. G = g0 g1 · · · gd−1 where d is the number of gates. A reversible gate has the form g(C, T ), where C = {xi1 , . . . , xik } ⊂ X is the set of control lines and T = {xj1 , . . . , xjl } ⊂ X with C ∩ T = ∅ is the set of target lines. C may be empty. The gate operation is applied to the target lines iff all control lines meet the required control conditions. Control lines and unconnected lines always pass through the gate unaltered. In the literature, three types of reversible gates have been established: • A (multiple control) Toffoli gate (MCT) [Tof80] has a single target line xj and maps (x1 , x2 , . . . , xj , . . . , xn ) to (x1 , x2 , . . . , xi1 xi2 · · · xik ⊕ xj , . . . , xn ). That is, a Toffoli gate inverts the target line iff all control lines are assigned to 1. • A (multiple control) Fredkin gate (MCF) [FT82] has two target lines xj1 and xj2 . The gate interchanges the values of the target lines iff the conjunction of all control lines evaluates to 1. • A Peres gate (P) [Per85] has a control line xi , a target line xj1 , and a line xj2 that serves as both, control and target. It maps (x1 , x2 , . . . , xj1 , . . . , xj2 , . . . , xn ) to (x1 , x2 , . . . , xi xj2 ⊕xj1 , . . . , xi ⊕xj2 , . . . , xn ) and thus is a cascade of two MCT gates. Example 2.4 Figure 2.2 shows a Toffoli gate (a), a Fredkin gate (b), and a Peres gate (c) together with a truth table of its functionality. A ● is used to indicate a control line, while an ⊕ (×) is used for denoting the target line of a Toffoli and Peres gate (Fredkin gate). Remark 2.1 These definitions also provide the basis for other gate types. For example, the Toffoli gate builds the basis for the NOT gate (a Toffoli gate with no control lines, i.e. with C = ∅), for the controlled-NOT gate (a Toffoli gate with one control
2.1 Background
11
Fig. 2.2 Reversible gates
Fig. 2.3 Reversible circuits
line),1 as well as for the Toffoli gate as originally proposed in [Tof80]. In contrast, the Fredkin gate builds the basis for a SWAP gate (a Fredkin gate with C = ∅, i.e. an interchanging of two lines). In the following, the notations MCT(C, xj ), MCF(C, xj1 , xj2 ), and P (xi , xj1 , xj2 ) are used to denote a Toffoli, Fredkin, and Peres gate, respectively. The number of control lines, a Toffoli (Fredkin) gates consists of, defines the size of the gate. Using these gate types, universal libraries can be composed. A gate library is called universal, if it enables the realization of any reversible function. For example, it has been proven that every reversible function can be realized using MCT gates only [MD04b]. Also, the gate library consisting of NOT, CNOT, and two-controlled Toffoli gates is universal [SPMH03]. In contrast, a library including only CNOT gates allows the realization of linear reversible functions only [PMH08]. Example 2.5 Figure 2.3 shows reversible circuits realizing the function depicted in Table 2.2(c) with the help of Toffoli and Fredkin gates, respectively. As for their traditional counterparts, the complexity of reversible circuits is measured by means of different cost metrics. More precisely, the cost of the respective circuits is defined as follows: 1 The
controlled-NOT gate is also known as CNOT or Feynman gate.
12
2 Preliminaries
Table 2.3 Quantum cost for Toffoli and Fredkin gates N O . OF CONTROL LINES
Q UANTUM COST OF A
T OFFOLI GATE
OF A
0
1
3
1
1
7
2
5
15
3
13
F REDKIN GATE
28, if at least 2 lines are unconnected 31, otherwise
4
26, if at least 2 lines are unconnected 29, otherwise
40, if at least 3 lines are unconnected 54, if 1 or 2 lines are unconnected 63, otherwise
5
6
38, if at least 3 lines are unconnected
52, if at least 4 lines are unconnected
52, if 1 or 2 lines are unconnected
82, if 1, 2 or 3 lines are unconnected
61, otherwise
127, otherwise
50, if at least 4 lines are unconnected
64, if at least 5 lines are unconnected
80, if 1, 2 or 3 lines are unconnected
102, if 1, 2, 3 or 4 lines are unconnected
125, otherwise
255, otherwise
Definition 2.7 A reversible circuit G = g0 g1 · · · gd−1 has cost of c=
d−1
ci ,
i=0
where ci denotes the cost of gate gi . The concrete cost for a single gate of course depends on the respective type but also on the addressed technology. In this book, the following cost metrics are used: • Gate count denotes the number of gates the circuit consists of (i.e. ci = 1 and c = d). • Quantum cost denotes the effort needed to transform a reversible circuit to a quantum circuit (see also next section). Table 2.3 shows the quantum cost for a selection of Toffoli and Fredkin gate configurations as introduced in [BBC+95] and further optimized in [MD04a] and [MYDM05]. As can be seen, gates of larger size are considerably more expensive than gates of smaller size. The Peres gate
2.1 Background
13
represents a special case, since it has quantum cost of 4, while the realization with two Toffoli gates would imply a cost of 6. • Transistor cost denotes the effort needed, to realize a reversible circuit in CMOS according to [TG08]. The transistor cost of a reversible gate is 8 · s where s is the number of control lines. Example 2.6 Consider the circuits from Example 2.5 depicted in Fig. 2.3. The Toffoli circuit has a gate count of 6, quantum cost of 10, and transistor cost of 56, while the Fredkin circuit has a gate count of 3, quantum cost of 13, and transistor cost of 8, respectively. As can be seen, the costs significantly differ depending on the applied cost model. Even if the number of gates in a cascade is a very simple measure of its complexity, it is the most technology-independent metric. Thus, the gate count is often used to evaluate the quality of a reversible circuit. Besides that, also the quantum cost metric is popular because it represents a measure for the most intensely studied application (namely quantum computation) and considers larger gates to be more costly. The transistor cost model is a relatively new model that arose with the application of reversible circuits to the area of low-power CMOS design. In this book, gate count and quantum cost are primarily considered as it allows a fair comparison of synthesis results with respect to previous work. Transistor costs are additionally addressed where appropriate. Finally, a special property of reversible logic is reviewed: Lemma 2.1 If the cascade of MCT gates G = g0 g1 · · · gd−1 realizes a reversible function f , then the reverse cascade G = gd−1 gd−2 · · · g0 realizes the inverse function f −1 . Proof Each reversible gate realizes a reversible function. That is, for each input pattern a unique output pattern, i.e. a one-to-one mapping, exists. Thus, calculating the inverse of the function f for an output pattern is essentially the same operation as propagating this pattern backwards through the circuit. This lemma is particularly exploited during synthesis of reversible logic as described later in this book. The next section considers quantum circuits and how they are derived from reversible logic.
2.1.3 Quantum Circuits Quantum computation [NC00] is a promising application of reversible logic. Every quantum circuit works on qubits instead of bits. In contrast to Boolean logic, qubits do not only allow to represent Boolean 0’s and Boolean 1’s, but also the superposition of both. More formally:
14
2 Preliminaries
Definition 2.8 A qubit is a two level quantum system, described by a two dimensional complex Hilbert space. The two orthogonal quantum states 1 0 |0 ≡ and |1 ≡ 0 1 are used to represent the Boolean values 0 and 1. Any state of a qubit may be written as |Ψ = α|0 + β|1, where α and β are complex numbers with |α|2 + |β|2 = 1. The quantum state of a single qubit is denoted by the vector α . β The state of a quantum system with n > 1 qubits is given by an element of the tensor product of the respective state spaces and can be represented as a normalized vector of length 2n , called the state vector. The state vector is changed through multiplication of appropriate 2n × 2n unitary matrices. Thus, each quantum computation is inherently reversible but manipulates qubits rather than pure logic values. At the end of the computation, a qubit can be measured. Then, depending on the current state of the qubit a 0 (with probability of |α|2 ) or a 1 (with probability of |β|2 ) returns, respectively. After the measurement, the state of the qubit is destroyed. In other words, using quantum computation and qubits in superposition, functions can be evaluated with different possible input assignments in parallel. But, it is not possible to obtain the current state of a qubit. Instead, if a qubit is measured, either 0 or 1 is returned depending on the respective probability. Nevertheless, researchers exploited quantum computation (in particular superposition) to solve many practically relevant problems faster than by traditional computing machines. For example, it was possible to solve the factorization problem in polynomial time— for traditional machines only exponential algorithms are known. Even if the research in this area is still at the beginning (so far, quantum algorithms with only up to 28 qubits have been implemented), these first promising results motivate further research in this area. The focus of this book is how to design reversible and quantum circuits, respectively. Thus, in the following the model for quantum circuits as used in this book is introduced. For a more detailed treatment of the respective physical background, the reader is referred to [Pit99, NC00, Mer07]. Definition 2.9 A quantum circuit Q is a cascade of quantum gates qi , i.e. Q = q0 · · · qd−1 . In this book, the following quantum gates are considered: • Inverter (NOT): A single qubit is inverted. • Controlled inverter (CNOT): The target qubit is inverted if the control qubit is 1. • Controlled V gate: A V operation is performed on the target qubit if the control qubit is 1. The V operation is also known as the square root of NOT, since two consecutive V operations are equivalent to an inversion.
2.1 Background
15
Fig. 2.4 Quantum gates
Fig. 2.5 State transitions for NOT, CNOT, V, and V+ operations
• Controlled V+ gate: A V+ operation is performed on the target qubit if the control qubit is 1. The V+ gate performs the inverse operation of the V gate, i.e. V + ≡ V −1 . The notation for these gates along with their corresponding 2n × 2n unitary matrices is shown in Fig. 2.4. In the following, the input to a quantum circuit as well as to each control line of a gate is restricted to 0 and 1. This has the effect that the value of each qubit is restricted to one value of the set {0, 1, V0 , V1 }, i.e. a 4-valued logic with 1+i 1 1 + i −i V0 = and V1 = −i 1 2 2 is applied. Figure 2.5 shows the resulting transitions with respect to the possible NOT, CNOT, V, and V+ operations. By restricting the quantum circuit model in this way, physical effects like superposition (and entanglement [NC00]) are excluded from the following consideration so that automated approaches (e.g. for synthesis, optimization, verification, etc.) become applicable. Nevertheless, the restricted model remains realistic for many applications. As an example, many of today’s quantum algorithms (e.g. Deutsch’s algorithm or Grover’s algorithm [NC00]) include quantum realizations of reversible (Boolean) functions. Thus, the mentioned restrictions are common in the design of quantum circuits.
16
2 Preliminaries
Fig. 2.6 Quantum circuit
Fig. 2.7 Pairs of quantum gates with unit cost
Example 2.7 Figure 2.6 shows a quantum circuit realizing the reversible function depicted in Table 2.2(c). All quantum gates are assumed to be the basic blocks of each quantum computation. This is also reflected in the cost metric. Definition 2.10 Each quantum gate has cost of 1. Thus, the cost of a quantum circuit is defined by the number d of its gates. Remark 2.2 In previous work, also an extended cost metric has been applied: When a CNOT and a V (or V+) gate are applied to the same two qubits, the cost of the pair can be considered unit as well [SD96, HSY+06]. The possible pairs (denoted by double gates in the following) are shown in Fig. 2.7. In this book, primarily the cost metric from Definition 2.10 is applied. However, all approaches can also be extended to consider unit cost of double gates. Exemplarily, this is shown for exact synthesis of quantum circuits in Sect. 4.2.2. Since quantum circuits are inherently reversible, every reversible circuit can be transformed to a quantum circuit. To this end, each gate of the reversible circuit is decomposed into a cascade of quantum gates. Example 2.8 Figure 2.8(a) (Fig. 2.8(b)) shows the quantum gate cascade which can be used to transform a Toffoli (Fredkin) gate to a quantum circuit. As can be seen, the number of required quantum gates is equal to the quantum cost of the Toffoli (Fredkin) gate as introduced in Table 2.3. Exploiting these decompositions, synthesis of quantum circuits can be approached from two different angles: (1) Targeting quantum gates directly during the synthesis process or (2) synthesizing reversible circuits first that are later mapped into quantum circuits.
2.2 Decision Diagrams To represent Boolean (including reversible) functions and circuits, decision diagrams can be applied. They provide an efficient data-structure that can represent
2.2 Decision Diagrams
17
Fig. 2.8 Decomposition of reversible gates to quantum circuits
large functions in a more compact way than truth tables. In the past, several types of decision diagrams have been introduced. In this book, Binary Decision Diagrams (BDDs) [Bry86] are considered to represent Boolean functions. Quantum Multiple-valued Decision Diagrams (QMDDs) [MT06, MT08] are used to represent reversible functions that may include quantum operations. Both are briefly introduced in this section.
2.2.1 Binary Decision Diagrams A Boolean function f : Bn → B can be represented by a graph-structure defined as follows: Definition 2.11 A Binary Decision Diagram (BDD) over Boolean variables X with terminals T = {0, 1} is a directed acyclic graph G = (V , E) with the following properties: 1. Each node v ∈ V is either a terminal or a non-terminal. 2. Each terminal node v ∈ V is labeled by a value t ∈ T and has no outgoing edges. 3. Each non-terminal node v ∈ V is labeled by a Boolean variable xi ∈ X and represents a Boolean function f . 4. In each non-terminal node (labeled by xi ), the Shannon decomposition [Sha38] f = x i fxi =0 + xi fxi =1 is carried out, leading to two outgoing edges e ∈ E whose successors are denoted by low(v) (for fxi =0 ) and high(v) (for fxi =1 ), respectively. The size of a BDD is defined by the number of its (non-terminal) nodes. Example 2.9 Figure 2.9 shows a BDD representing the function f = x1 ⊕ x2 · x3 . Edges leading to a node fxi =0 (fxi =1 ) are marked by a 0 (1). This BDD has a size of 5.
18
2 Preliminaries
Fig. 2.9 BDD representing f = x1 ⊕ x2 · x3
A BDD is called free if each variable is encountered at most once on each path from the root to a terminal node. A BDD is called ordered if in addition all variables are encountered in the same order on all such paths. The respective order is defined by π : {1, . . . , n} → {1, . . . , n}. Finally, a BDD is called reduced if it does neither contain isomorphic sub-graphs nor redundant nodes. To achieve reduced BDDs, reduction rules as depicted in Fig. 2.10 are applied. Applying the reduction rules leads to shared nodes, i.e. nodes that have more than one predecessor. Example 2.10 Figure 2.11 shows two reduced ordered BDDs representing the function f = x1 ·x2 +x3 ·x4 +· · ·+xn−1 ·xn . For the order x1 , x2 , . . . , xn−1 , xn , the BDD depicted in Fig. 2.11(a) has a size of O(n), while the BDD depicted in Fig. 2.11(b) with the order x1 , x3 , . . . , xn−1 , x2 , x4 , . . . , xn has size of O(2n ). Remark 2.3 In the following, reduced ordered binary decision diagrams are called BDDs for brevity. BDDs are canonical representations, i.e. for a given Boolean function and a fixed order, the BDD is unique [Bry86]. As shown by Example 2.10, BDDs are very sensitive to the chosen variable order. It has been shown in [BW96] that proving the existence of a BDD with a lower number of nodes (i.e. proving that no other order leads to a smaller BDD size) is NP-complete. As a consequence, several heuristics to find good orders have been proposed. In particular, sifting [Rud93] has been shown to be quite effective. Further reductions of the BDD size can be achieved, if complement edges [BRB90] are applied. They allow to represent a function as well as its complement by one single node only. BDDs can also be used to represent multi-output functions. Then, all BDDs for the respective functions are shared, i.e. isomorphic sub-functions are represented by a single node as well. For a more comprehensive introduction into BDDs, the reader is refereed to [DB98, EFD05]. For the application of BDDs in practice, many well-engineered BDD packages (e.g. CUDD [Som01]) are available.
2.2 Decision Diagrams
19
Fig. 2.10 Reduction rules for BDDs
Fig. 2.11 BDDs with different variable orders
2.2.2 Quantum Multiple-valued Decision Diagrams As described in Sect. 2.1.3, quantum operations are defined by 2n × 2n unitary matrices (consider again Fig. 2.4 on p. 15 for examples). Thus, to represent functions including quantum operations, an adjusted data-structure is needed. Quantum Multiple-valued Decision Diagrams (QMDDs) [MT06, MT08] provide for the representation and manipulation of r n × r n complex-valued matrices with r pure logic states. This includes unitary matrices and thus QMDDs can be applied to represent quantum gates and circuits. Since in this book QMDDs are used as black box only (in contrast to BDDs), a formal definition of QMDDs is omitted and instead they are introduced by exemplarily describing the general idea.
20
2 Preliminaries
Fig. 2.12 QMDD representing the matrix of a single V gate
A QMDD structure is based on partitioning an r n × r n matrix M into r 2 submatrices, each of dimension r n−1 × r n−1 as shown in the following equation: ⎛ ⎞ M0 M1 · · · Mr−1 ⎜ ⎟ ⎜ Mr Mr+1 · · · M2r−2 ⎟ ⎜ ⎟ . M =⎜ . .. .. ⎟ .. ⎜ .. . . . ⎟ ⎝ ⎠ Mr 2 −r Mr 2 −r+1 · · · Mr 2 −1 In the following, the concepts of QMDDs are briefly presented by way of the following example of a single V gate. Example 2.11 Figure 2.12(a) shows a V gate in a 3-line circuit. The unitary matrix describing the behavior of this gate is given in Fig. 2.12(b) where v = 1+i 2 and v = 1−i . The QMDD for this matrix is given in Fig. 2.12(c). The edges from each 2 non-terminal node point to four sub-matrices indexed 0, 1, 2, 3 from left to right. Each edge has a complex-valued weight. For clarity, edges with weight 0 are indicated as stubs. In fact, they point to the terminal node. The key features of QMDD are evident in this example. There is a single terminal node. Furthermore, each edge has a complex-valued weight. Each non-terminal node represents a matrix partitioning. For example, the top node in Fig. 2.12(c) represents the partitioning shown in Fig. 2.12(b). The non-terminal nodes lower in the diagram represent similar partitioning of the resulting sub-matrices. The representation of common sub-matrices is shared. To ensure the uniqueness of the representation, edges with weight 0 must point to the terminal node and normalization is applied to non-terminal nodes so that the lowest indexed edge with non-zero weight has weight 1. As for BDDs, an efficient implementation exists also for QMDDs. However, since QMDD involve multiple edges from nodes and are applicable to both binary
2.3 Satisfiability Solvers
21
and multiple-valued problems, the QMDD package is not built using a standard decision diagram package. Nevertheless, the implementation employs well-known decision diagram techniques like sharing, reordering, and so on. For a more comprehensive introduction into QMDDs, the reader is referred to [MT08].
2.3 Satisfiability Solvers The methods described in this book make use of techniques for solving the Boolean satisfiability problem (SAT problem). The SAT problem is one of the central NP-complete problems. In fact, it was the first known NP-complete problem that was proven by Cook in 1971 [Coo71]. Despite this proven complexity, efficient solving algorithms have been developed that found great success as proof engines for many practically relevant problems. Today there exists algorithms exploiting SAT that solve many practical problem instances, e.g. in the domain of automatic test pattern generation [Lar92, DEF+08], logic synthesis [ZSM+05], debugging [SVAV05], and verification [BCCZ99, CBRZ01, PBG05]. In this section, the SAT problem, the respective solving algorithm, and its application are introduced. Furthermore, extended SAT solvers additionally exploiting bit-vector logic, quantifiers, or problem-specific modules, respectively, are briefly reviewed. These engines are used later as core techniques for selected steps in the proposed flow for reversible logic.
2.3.1 Boolean Satisfiability The Boolean satisfiability problem (SAT problem) is defined as follows: Definition 2.12 Let h : Bn → B be a Boolean function. Then, the SAT problem is to find an assignment to the variables of h such that h evaluates to 1 or to prove that no such assignment exists. In other words, SAT asks if ∃Xh for an h over variables X and determines a satisfying assignment in this case. In this context, the Boolean formula h is often given in Conjunctive Normal Form (CNF). A CNF is a set of clauses, each clause is a set of literals, and each literal is a Boolean variable or its negation. The CNF formula is satisfied if all clauses are satisfied, a clause is satisfied if at least one of its literals is satisfied, and a variable is satisfied when 1 is assigned to the variable (the negation of a variable is satisfied under the assignment 0). Example 2.12 Let h = (x1 + x2 + x 3 )(x 1 + x3 )(x 2 + x3 ). Then, x1 = 1, x2 = 1, and x3 = 1 is a satisfying assignment for h. The values of x1 and x2 ensure that the first clause becomes satisfied, while x3 ensures this for the remaining two clauses.
22
2 Preliminaries
Fig. 2.13 Solving algorithm in modern SAT solvers
To solve SAT problems, in the past several (backtracking) algorithms (or SAT solvers, respectively) have been proposed [DP60, DLL62, MS99, MMZ+01, GN02, ES04]. Most of them apply the steps depicted in Fig. 2.13: While there are free variables left (a), a decision is made (c) to assign a value to one of these variables. Then, implications are determined due to the last assignment (d). This may cause a conflict (e) that is analyzed. If the conflict can be resolved by undoing assignments from previous decisions, backtracking is done (f). Otherwise, the instance is unsatisfiable (g). If no further decision can be made, i.e. a value is assigned to all variables and this assignment did not cause a conflict, the CNF is satisfied (b). Advanced techniques like e.g. efficient Boolean constraint propagation [MMZ+01] or conflict analysis [MS99] as well as efficient decision heuristics [GN02] are common in state-of-the-art SAT solvers today. These techniques as well as the tremendous improvements in the performance of the respective implementations [ES04] enable the consideration of problems with more than hundreds of thousands of variables and clauses. Thus, SAT is widely used in many application domains. Therefore, the real world problem is transformed into CNF and then solved by using a SAT solver as a black box.
2.3.2 Extended SAT Solvers Despite their efficiency, Boolean SAT solvers have a major drawback: they work on the Boolean level. But, many problems are formulated on a higher level of abstraction and would benefit from a more general description, respectively. As a consequence, researchers investigated the use of more expressive formulations than CNF—by still exploiting the established SAT techniques. This leads (1) to the combination of SAT solvers with decision procedures for decidable theories resulting
2.3 Satisfiability Solvers
23
in SAT Modulo Theories (SMT) [BBC+05, DM06b] and (2) to the application of quantifiers resulting in Quantified Boolean Formulas (QBF) [Bie05, Ben05]. Furthermore, problem-specific knowledge is exploited during the solving process by the SAT solver SWORD [WFG+07]. The respective concepts are briefly reviewed in the following.
2.3.2.1 SMT Solvers for Bit-vector Logic An SMT solver integrates a Boolean SAT solver with other solvers for specialized theories (e.g. linear arithmetic or bit-vector logic). The SAT solver thereby works on an abstract representation (still in CNF) of the problem and steers the overall search process, while each (partial) assignment of this representation has to be validated by the theory solver for the theory constraints. Thus, advanced SAT techniques together with specialized theory solvers are exploited. In this book, the theory of quantifier free bit-vector logic (QF_BV) is utilized. This logic is defined as follows: Definition 2.13 A bit-vector is an element b = (bn−1 , . . . , b0 ) ∈ Bn . The index [ ] : Bn × [0, n) → B maps a bit-vector b and an index i to the ith component of the vector, i.e. b[i] = bi . Conversion from (to) a natural number isdefined by i nat : Bn → N (bv : N → Bn ) with N = [0, 2n ) ⊂ N and nat(b) := n−1 i=0 bi · 2 −1 (bv := nat ). Problems can be constraint by using bit-vector operations as well as arithmetic operations. Let a, b ∈ Bn be two bit-vectors. Then, the bit-vector operation ◦ ∈ {∧, ∨, . . .} is defined by a ◦ b := (a[n − 1] ◦ b[n − 1], . . . , a[0] ◦ b[0]). An arithmetic operation • ∈ {·, +, . . .} is defined by a • b := nat(a) • nat(b). Example 2.13 Let a, b, and c be three bit-vector variables with bit-width n = 3 and (a ∨ b = c) ∧ (a + b = c) an SMT bit-vector instance over these variables. Then, a = (010), b = (001), and c = (011) is a satisfying solution of this instance, since it satisfies each constraint. To solve SMT instances in QF_BV logic either (1) a combination of a traditional SAT solver and a specialized (bit-vector) theory solver is applied (see e.g. [BBC+05, DM06a]), (2) the instance is pre-processed exploiting the higher level of abstraction before the resulting (simplified) instance is bit-blasted to a traditional SAT solver (see e.g. [GD07, BB09]), or (3) a specialized solver that directly works on the bitlevel of the problem is used (see e.g. [DBW+07]). Having an efficient solver available, similar to Boolean SAT the real world problem is transformed into a QF_BV instance. But instead of a description in terms of clauses, the higher level representation in terms of bit-vectors is used. Then, the resulting instance is passed to the solver which is again used as a black box. The higher abstraction which is now available can be exploited to accelerate the solving process.
24
2 Preliminaries
2.3.2.2 QBF Solvers Another generalization of the SAT problem is given by QBF satisfiability. Here, variables of the Boolean function h additionally can be universally and existentially quantified. More formally: Definition 2.14 Let h : Bn → B be a Boolean function over the variables X (usually given in CNF). Then, Q1 X1 . . . Qt Xt h with disjunct Xi ⊂ X and Qi ∈ {∃, ∀} is a Quantified Boolean Formula (QBF). The QBF problem is to find an assignment to the variables of h such that h evaluates to 1 with respect to the quantifiers or to prove that no such assignment exists. Example 2.14 Let ∃x2 , x3 ∀x1 (x1 + x2 + x 3 )(x 1 + x3 )(x 2 + x3 ). Then x2 = 1 and x3 = 1 is a satisfying assignment for the QBF h. The value of x2 ensures that the first clause becomes satisfied, while x3 ensures this for the remaining two clauses for all possible assignments to x1 . Obviously, solving QBF problems is significantly harder than solving pure SAT instances. In fact, it is PSPACE-complete [Pap93]. Nevertheless, QBF enables the formulation of many problems in a more compact way. In this sense, complexity is moved from the problem formulation to the solving engine, i.e. the task can be formulated in a more compact way resulting in a more complex problem to be solved by the solver. However, since usually solving engines are well-engineered with respect to the dedicated problem, this may lead to a faster solving process. Today, recent solvers (e.g. [Bie05, Ben05]) exploit techniques like symbolic skolemization to solve QBF instances (i.e. converting the instance to a normal form which enables simplifications).
2.3.2.3 SWORD Solver Due to the translation of the problem into CNF (or QF_BV logic, respectively), problem-specific knowledge is lost. More illustrative, decisions, implications, and learning schemes can only exploit the Boolean (bit-vector) description. In contrast, with more problem-specific knowledge available, more options exist how to control the search space traversion. This observation is exploited by the problem-specific SAT solver SWORD [WFG+07].2 SWORD represents the problem in terms of so called modules. Each module defines an operation over bit vectors of module variables. Each module variable is a Boolean variable. By this, structural and semantical knowledge is available which can be exploited by special algorithms for each kind of module. Furthermore, this 2 SWORD
has been co-developed by the authors of this book. Even if SWORD is focused on problem-specific knowledge, it can also be used as an SMT solver and already participated in the respective SMT competitions in 2008 [WSD08] and 2009 [JSWD09], respectively.
2.3 Satisfiability Solvers
25
Fig. 2.14 SWORD algorithm
leads to a more compact problem formulation, since representing complex operations in terms of modules substitutes a significant amount of clauses. Example 2.15 Consider an n × n-multiplier. This multiplier can be represented by n2 AND gates and n − 1 adders [MK04]. Furthermore, a single AND gate can be modeled by three clauses and requires θ (n2 ) auxiliary variables. Thus, just to encode the AND gates a CNF with θ (n2 ) auxiliary variables and clauses is required, respectively. In contrast, using SWORD only 3n module variables (for the two inputs and the output of the multiplication) and a single (multiplier) module are needed to represent the whole multiplication. Given a SAT instance including modules, the overall algorithm depicted in Fig. 2.14 is used to solve the problem. This algorithm is similar to the procedure as applied in standard SAT solvers (see Fig. 2.13 on p. 22): While free variables remain (a), a decision is made (c), implications resulting from this decision are carried out (d), and if a conflict occurs, it is analyzed (f). The important difference is that SWORD has two operation levels: The global algorithm controls the overall search process and calls local procedures of modules for decision and implication. Thus, decision making and the implication engine can be adjusted for each type of module. In more detail, the solver first chooses a particular module based on a global decision heuristic (c.1). Then, this module chooses a value for one of its variables according to a local decision heuristic (c.2). Afterwards, the solver calls the local implication procedures (d.2) of all modules that are potentially affected (d.1) by the previous decision or implication. Here, a variable watching scheme similar to the one presented in [MMZ+01] is used which can efficiently determine these modules. The chosen modules imply further assignments and detect conflicts.
26
2 Preliminaries
Due to the two operation levels, problem-specific strategies e.g. for decision making and propagation can be exploited by the modules. For example, decision making can be prioritized so that modules, which are assumed to be “more important” than others, are selected for a decision with a higher priority than less important modules. Furthermore, different modules can be equipped with different strategies. For a more detailed description of SWORD, the reader is referred to [WFG+07, WFG+09]. Therewith, all preliminaries required for this book have been introduced. Besides an introduction of reversible and quantum logic, also the applied core techniques have been briefly described. With that as a basis, the contributions towards a design flow for reversible logic are proposed in the following chapters. Decision diagrams are thereby applied for synthesis (Sect. 3.2), partially for exact synthesis (Sect. 4.3.2), and for verification (Sect. 7.1.2), while Boolean satisfiability is exploited in exact synthesis (especially in Sect. 4.2 and partially in Sect. 5.3 as well as Sect. 6.3), verification (Sect. 7.1.3), and debugging (Sect. 7.2). The extended SAT solvers (i.e. SMT solvers, QBF solvers, and SWORD) are used to improve exact synthesis (Sect. 4.3). Note on Benchmarks In the following chapters, the respective contributions are introduced and evaluated in detail. Different scopes are thereby considered that are also reflected in the benchmark sets used to experimentally evaluate the respective methods. Synthesis approaches are evaluated using large functions (for the proposed heuristic method), small functions (for exact synthesis), and irreversible functions (to evaluate the different embeddings). In contrast, the methods targeting optimization, verification, and debugging work on a given circuit description. Furthermore, different timeouts are applied in the respective evaluations (e.g. exact synthesis normally requires more run-time than heuristic synthesis). As already stated in the introduction, the benchmarks used in this book are publicly available at www.revlib.org. The resulting tools can be obtained under www.revkit.org.
Chapter 3
Synthesis of Reversible Logic
Synthesis is the most important step while building complex circuits. Considering the traditional design flow, synthesis is carried out in several individual steps such as high-level synthesis, logic synthesis, mapping, and routing (see e.g. [SSL+92]). To synthesize reversible logic, adjustments and extensions are needed. For example, further tasks such as embedding of irreversible functions must be added. Furthermore, throughout the whole flow, the restrictions caused by the reversibility (no fanout and feedback) and a completely new gate library must be considered as well. In the last years, first approaches addressing some of these issues have been introduced (see e.g. [SPMH03, MMD03, MD04b, Ker04, MDM05, GAJ06, HSY+06, MDM07]). The first section of this chapter briefly reviews existing methods for the individual steps. However, the research in this area is still at the beginning. So far, the desired behavior of the circuit to be synthesized is given by function descriptions like truth tables or permutations, respectively. As a result, current synthesis methods are applicable to relatively small functions only and often need a significant amount of run-time. This must be improved in order to design larger functions or complex reversible systems in the future. In this book, the wide area of reversible logic synthesis is covered by the following three chapters—each chapter with an own detailed view on a particular aspect. While Chap. 4 introduces exact (i.e. minimal) circuit synthesis, Chap. 5 discusses aspects of embedding in detail. The present chapter builds the basis for them and additionally proposes new methods that allows a fast synthesis of significantly larger functions and more complex circuits, respectively. Since Toffoli circuits as introduced in Sect. 2.1.2 generally build the basis for both, reversible and quantum circuits, in the following, the focus is on synthesis of Toffoli cascades. Nevertheless, quantum circuit synthesis is additionally considered when it is appropriate. As already mentioned, the first part of this chapter builds the basis for all remaining synthesis sections. Here, it is shown how irreversible functions must be embedded into reversible ones before they can be applied to existing synthesis methods. Then, by using the example of the transformation-based approach introduced in [MMD03], one of the previous synthesis methods is described and discussed. Altogether, this briefly summarizes the basic synthesis steps for reversible logic as they exist today. R. Wille, R. Drechsler, Towards a Design Flow for Reversible Logic, DOI 10.1007/978-90-481-9579-4_3, © Springer Science+Business Media B.V. 2010
27
28
3 Synthesis of Reversible Logic
Motivated by this (in particular by the limitations of the current synthesis methods), the second part of this chapter introduces a new synthesis approach [WD09, WD10] that exploits Binary Decision Diagrams (BDDs) [Bry86]. BDDs allow an efficient representation of large Boolean functions that can be mapped into reversible cascades. As a result, for the first time Toffoli circuits for functions containing over 100 variables can be derived efficiently. Finally, how to specify and synthesize more complex reversible circuits at higher abstractions is considered in the third part of this chapter. For this purpose, a new programming language (called SyReC) and a respective hierarchical synthesis approach is presented and evaluated [WOD10].
3.1 Current Synthesis Steps This section illustrates the current synthesis steps that use well-established methods. First, the problem of embedding irreversible functions is considered. Second, the synthesis itself is introduced. For the latter, a widely known approach, namely the transformation-based approach introduced in [MMD03], is used. Most of the remaining synthesis methods apply similar strategies (e.g. [Ker04, GAJ06, MDM07]) or are developed on top of this method (e.g. [MDM05]).
3.1.1 Embedding Irreversible Functions Table 3.1 shows the truth table of a 1-bit adder which is used as an example in this section. The adder has three inputs (the carry-in cin as well as the two summands x and y) and two outputs (the carry-out cout and the sum). The adder obviously is irreversible, since • the number of inputs differs from the number of outputs and • there is no unique input-output mapping. Even adding an additional output to the function (leading to the same number of inputs and outputs) would not make the function reversible. Then, without loss of generality, the first four lines of the truth table can be embedded with respect to reversibility as shown in the rightmost column of Table 3.1. However, since cout = 0 and sum = 1 already appeared two times (marked bold), no unique embedding for the fifth line is possible any longer. The same also holds for the lines marked italic. This has already been observed in [MD04b]. Here, the authors came to the conclusion that at least log2 (μ) additional (garbage) outputs are required to make an irreversible function reversible, where μ is the maximum number of times an output pattern is repeated in the truth table. Since for the adder at most three output patterns are repeated, log2 (3) = 2 additional outputs are required to make the function reversible.
3.1 Current Synthesis Steps Table 3.1 Truth table of an adder
Table 3.2 Truth table of an embedded adder
29 cin
x
y
cout
sum
0
0
0
0
0
0
0
0
1
0
1
0
0
1
0
0
1
1
0
1
1
1
0
0
1
0
0
0
1
?
1
0
1
1
0
1
1
1
0
1
0
?
1
1
1
1
1
1
0
cin
x
y
cout
sum
g1
g2
0
0
0
0
0
0
0
0
0
0
0
1
0
1
1
1
0
0
1
0
0
1
1
0
0
0
1
1
1
0
0
1
0
1
0
0
0
1
0
0
0
1
0
1
1
0
1
1
0
1
1
0
1
0
1
0
0
1
1
1
1
1
0
1
1
0
0
0
1
0
0
0
1
0
0
1
1
1
1
1
1
0
1
0
1
1
1
0
1
0
1
1
0
0
0
1
1
1
0
0
1
1
0
0
1
1
0
1
0
0
1
1
1
1
1
0
0
0
1
0
1
1
1
1
0
1
0
1
Adding new lines causes constant inputs and garbage outputs. The value of the constant inputs can be chosen by the designer. Garbage outputs are by definition don’t cares and thus can be left unspecified leading to an incompletely specified function. However, many synthesis approaches require a completely specified function so that often all don’t cares must be assigned to a concrete value. As a result, the adder is embedded in a reversible function including four variables, one constant input, and two garbage outputs. A possible assignment to the constant as well as the don’t care values is depicted in Table 3.2 (where the original adder function is marked bold). In the following, a synthesis method is introduced assuming a completely specified reversible function as input. However, the concrete embedding of irreversible functions (in particular the concrete assignment to don’t
30
3 Synthesis of Reversible Logic
Table 3.3 MDM procedure line
input
output
1st step
2nd step
3rd step
4th step
5th step
6th step
(i)
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
0
0000
0000
0000
0000
0000
0000
0000
0000
1
0001
0111
0101
0001
0001
0001
0001
0001
2
0010
0110
0110
0110
0010
0010
0010
0010
3
0011
1001
1011
1111
1011
0011
0011
0011
4
0100
0100
0100
0100
0100
0100
0100
0100
5
0101
1011
1001
1101
1101
1101
0101
0101
6
0110
1010
1010
1010
1110
1110
1110
0110
7
0111
1101
1111
1011
1111
0111
1111
0111
8
1000
1000
1000
1000
1000
1000
1000
1000
9
1001
1111
1101
1001
1001
1001
1001
1001
10
1010
1110
1110
1110
1010
1010
1010
1010
11
1011
0001
0011
0111
0011
1011
1011
1011
12
1100
1100
1100
1100
1100
1100
1100
1100
13
1101
0011
0001
0101
0101
0101
1101
1101
14
1110
0010
0010
0010
0110
0110
0110
1110
15
1111
0101
0111
0011
0111
1111
0111
1111
cares) can have a significant impact on the synthesis results (i.e. on the number of gates in the resulting circuit). Thus, this issue is again considered in Chap. 5 which also provides examples showing the effect of different embeddings.
3.1.2 Transformation-based Synthesis In this section, synthesis of reversible logic is exemplarily described using the approach from [MMD03]. The basic idea is to traverse each line of the truth table and to add gates to the circuit until the output values match the input values (i.e. until the identity is achieved). Gates are thereby chosen so that they don’t alter already considered lines. Furthermore, gates are added starting at the output side of the circuit (this is because output values are transformed until the identity is achieved). In the following, the approach is described using the example of the embedded adder from Table 3.2. Table 3.3 shows the respective steps. The first column denotes the truth table line numbers, while the second and third column give the function specification of the adder. For brevity, the inputs 0, cin , x, y and the outputs cout , sum, g1 , g2 are denoted by a, b, c, d, respectively. The remaining columns provide the transformed output values for the respective steps. The approach starts at truth table line 0. Since for this line the input is already equal to the output (both are assigned to 0000), no gate has to be added. In contrast, to match the output with the input in line 1, the values for c and b must be
3.2 BDD-based Synthesis
31
Fig. 3.1 Circuit obtained by transformation-based synthesis
inverted. To this end, two gates MCT({d}, c) (1st step) and MCT({d}, b) (2nd step) are added as depicted in Fig. 3.1. Due to the control line d, this does not affect the previous truth table line. In line 2 and line 3, an MCT({c}, b) as well as an MCT({c, d}, a) is added to match the values of b and a, respectively (step 3 and 4). For the latter gate, two control lines are needed to keep the already traversed truth table lines unaltered. Afterwards, only two more gates MCT({d, b}, a) (5th step) and MCT({c, b}, a) (6th step) are necessary to achieve the input-output identity. The resulting circuit is shown in Fig. 3.1. This circuit consists of six gates and has quantum cost of 18. In [MMD03], further variations of this approach are discussed. In fact, this transformation can also be applied in the inverse direction (i.e. so that the input must match the output) and in both directions simultaneously. Furthermore, in [MDM05] the approach has been extended by the application of templates. These help to reduce the size of the resulting circuits and thus to achieve circuits with lower cost. Having this as a general introduction into synthesis of reversible logic, in the following new synthesis approaches are proposed.
3.2 BDD-based Synthesis The strategy introduced in the last section (namely selecting reversible gates so that the chosen function representation becomes the identity) has been adopted and extended by many other researchers. More precisely, more compact data-structures like decision diagrams [Ker04], positive-polarity Reed-Muller expansion [GAJ06], or Reed-Muller spectra [MDM07] have been applied. But even if complementary approaches are used (e.g. [SPMH03]), so far all approaches are applicable only to relatively small functions, i.e. functions with at most 30 variables [GAJ06]. Moreover, often a significant amount of run-time is needed to achieve these results. Thus, current synthesis methods are limited. These limitations are caused by the underlying techniques. The existing synthesis approaches often rely on truth tables (or similar descriptions like permutations) of the function to be synthesized (e.g. in [SPMH03, MMD03]). But even if alternative data-structures (e.g. the ones mentioned above) are used, the same limitations can be observed. In this section, a synthesis method that can cope with significantly larger functions is introduced. The basic idea is as follows: First, for the function to be synthesized a BDD (see Sect. 2.2.1) is built. This can be efficiently done for large functions using existing well-developed techniques. Then, each node of the BDD is substituted by a cascade of reversible gates. Since BDDs may include shared nodes
32
3 Synthesis of Reversible Logic
causing fanouts (which are not allowed in reversible logic), this may require additional circuit lines. As a result, circuits composed of Toffoli or quantum gates, respectively, are obtained in time and with memory linear to the size of the BDD. Moreover, since the size of the resulting circuit is bounded by the BDD size, theoretical results known from BDDs (see e.g. [Weg00, LL92]), can be transferred to reversible circuits. The experiments show significant improvements (with respect to the resulting circuit cost as well as to the run-time) in comparison to previous approaches. Furthermore, for the first time large functions with more than hundred variables can be synthesized at very low run-time. In the remainder of this section, the BDD-based synthesis approach is introduced as follows: In Sect. 3.2.1, the general idea and the resulting synthesis approach is described in detail. How to exploit BDD optimizations is shown in Sect. 3.2.2, while Sect. 3.2.3 briefly reviews some of the already known theoretical results from reversible logic synthesis and introduces bounds which follow from the new synthesis approach. Finally, in Sect. 3.2.4 experimental results are given.
3.2.1 General Idea In this section, the general idea of the BDD-based synthesis is proposed. The aim of the approach is to determine a circuit realization for a given Boolean function. It is well known that Boolean functions can be efficiently represented by BDDs. Given a BDD G = (V , E), a reversible circuit can be derived by traversing the decision diagram and substituting each node v ∈ V with a cascade of reversible gates. The concrete cascade of gates depends on whether the successors of the node v are terminals or not. For the general case (no terminals), the first row of Table 3.4 shows a substitution with two Toffoli gates or five quantum gates, respectively. The following rows give the substitutions for the remaining cases. These cascades can be applied to derive a complete Toffoli circuit (or quantum circuit, respectively) from a BDD without shared nodes. Example 3.1 Consider the BDD in Fig. 3.2(a). Applying the substitutions given in Table 3.4 to each node of the BDD, the Toffoli circuit depicted in Fig. 3.2(b) results. Remark 3.1 As shown in Table 3.4, an additional (constant) line is necessary if one of the edges low(v) or high(v) leads to a terminal node. This is because of the reversibility which has to be ensured when synthesizing reversible logic. As an example consider a node v with high(v) = 0 (second row of Table 3.4). Without loss of generality, the first three lines of the corresponding truth table can be embedded with respect to reversibility as depicted in Table 3.5(a). However, since f is 0 in the last line, no reversible embedding for the whole function is possible. Thus, an additional line is required to make the respective substitution reversible (see Table 3.5(b)).1 1 For the same reason, it is also not possible to preserve the values for low(v) or high(v), respectively, in the substitution depicted in the first row of Table 3.4.
3.2 BDD-based Synthesis
Table 3.4 Substitution of BDD nodes to reversible/quantum circuits
33
34
3 Synthesis of Reversible Logic
Fig. 3.2 BDD and Toffoli circuit for f = x1 ⊕ x2
Table 3.5 (Partial) Truth tables for node v with high(v) = 0
(b) with additional line
(a) w/o add. line xi
low(f )
f
–
0 xi low(f )
f xi low(f )
0
0
0
0
0 0 0
0 0 0
0
1
1
1
0 0 1
1 0 1
1
0
0
1
0 1 0
0 1 0
1
1
0
?
0 1 1
0 1 1
Based on these substitutions, a method for synthesizing Boolean functions in reversible or quantum logic can be formulated: First, a BDD for function f to be synthesized is created. This can be done efficiently using state-of-the-art BDD packages (e.g. CUDD [Som01]). Next, the resulting BDD G = (V , E) is processed by a depth-first traversal. For each node v ∈ V , cascades as depicted in Table 3.4 are added to the circuit. As a result, circuits are synthesized that realize the given function f .
3.2.2 Exploiting BDD Optimization To build compact BDDs, current state-of-the-art BDD packages exploit several optimization techniques such as shared nodes [Bry86], complement edges [BRB90], or reordering [Bry86, Rud93]. In this section, it is shown how these techniques can be applied to the proposed BDD-based synthesis.
3.2.2.1 Shared Nodes If a node v has more than one predecessor, then v is called a shared node. The application of shared nodes is common for nearly all BDD packages. Shared nodes can be used to represent a sub-formula more than once without the need to rebuild the whole sub-graph. In particular, functions f : Bn → Bm (i.e. functions with more than one output) can be represented more compactly using shared nodes.
3.2 BDD-based Synthesis
35
Fig. 3.3 Substitution for shared nodes without terminals as successors
However, to apply shared nodes in reversible logic synthesis, the output value of a respective node has to be preserved until it is not needed any longer. Considering the substitutions depicted in Table 3.4, this holds for all cases where one of the edges low(v) or high(v) lead to a terminal node. Here, all values of the inputs (in particular of high(v) or low(v) that represent output values of other nodes) are preserved. In contrast, this is not the case for the general case (first row of Table 3.4). Here, only one value (namely the value from the select variable xi ) is preserved. Thus, a modified substitution for shared nodes without terminals as successors is required. Figures 3.3(a) and 3.3(b) show one possible substitution to a reversible cascade and a quantum cascade, respectively. Besides an additional constant circuit line, this requires one (three) additional reversible gates (quantum gates) in comparison to the substitution of Table 3.4. But therefore, shared nodes are supported. Moreover, this substitution also allows to represent the identity of a select variable (last row of Table 3.4) by the respective input line of the circuit (i.e. without any additional gates or lines). Previously, this was not possible, since the value of this circuit line was not necessarily preserved (as an example see Fig. 3.2 where the value of the identity node f gets lost after node f is substituted). Exploiting this, the synthesis algorithm proposed in the last section can be improved as follows: Again a BDD for the function to be synthesized is build which is afterwards traversed in a depth-first manner. Then, for each node v ∈ V , the following checks are performed: 1. Node v represents the identity of a primary input (i.e. the select input) In this case no cascade of gates is added to the circuit, since the identity can be represented by the same circuit line as the input itself. 2. Node v contains at least one edge (low(v) or high(v), respectively) leading to a terminal In this case substitutions as depicted in Table 3.4 are applied, since they often need a smaller number of gates and additionally preserve the values of all input signals. 3. The values of low(v) and high(v) are still needed, since they represent either shared nodes or the identity of an input variable In this case the substitutions depicted in Fig. 3.3 are applied, since they preserve the values of all input signals. 4. Otherwise The substitution as depicted in the first row of Table 3.4 is applied, since no input values must be preserved or a terminal successor occurs, respectively. In
36
3 Synthesis of Reversible Logic
Fig. 3.4 Toffoli circuits for shared BDD
this case, the smaller cascades (with respect to both the number of additional lines and the number of gates) are preferred. Example 3.2 In Fig. 3.4(a) a partial BDD including a shared node f is shown. Since the value of node f is used twice (by nodes f1 and f2 ), an additional line (the second one in Fig. 3.4(b)) and the cascade of gates as depicted in Fig. 3.3 are applied to substitute node f1 . Then, the value of f is still available such that the substitution of node f2 can be applied. The resulting circuit is given in Fig. 3.4(b). Figure 3.4(c) shows the resulting circuit for low(f ) = 0 and high(f ) = 1, i.e. for f representing the identity of xj . In this case no gates for f are added. Instead, the fifth line is used to store the value for both, xj and f . Besides that, the remaining substitutions are equal to the ones described above.
3.2.2.2 Complement Edges Further reductions in BDD sizes can be achieved if complement edges [BRB90] are applied. In particular, this allows to represent a function as well as its negation by a single node only. If there is a complement edge e.g. between v and low(v), then Shannon decomposition with an inverted value of low(v) is applied. To support
3.2 BDD-based Synthesis
37
complement edges in the proposed synthesis approach, adjusted substitutions have to be used that take the inversion of complemented edges into account. Table 3.6 shows the resulting cascades used in the proposed synthesis approach. Note that complements have to be considered only at the low edges of the nodes, since complements at high-edges can be mapped to them and vice versa. In some cases, this leads to larger cascades in comparison to the substitution without complement edges (e.g. compare the second row of Table 3.6 to the first row of Table 3.4). How far this can be compensated by the possible BDD reductions is discussed in the experimental evaluation in Sect. 3.2.4.
3.2.2.3 Reordering of BDDs Finally, different BDD orders may influence the synthesis results. It has been shown that the order of the variables has a high impact to the size of the resulting BDD [Bry86] (see e.g. Fig. 2.11 on p. 19). Since reducing the number of nodes may also reduce the size of the resulting circuits, reordering is considered in this section. In the past, several approaches have been proposed to achieve good orders (e.g. sifting [Rud93]) or to determine exact results (e.g. [DDG00]) with respect to the number of nodes. All these techniques can be directly applied to the BDD-based synthesis approach and need no further adjustments of the already introduced substitutions. Using these optimization techniques (i.e. shared nodes, complement edges, and reordering), in Sect. 3.2.4 it is considered how they influence the resulting Toffoli or quantum circuits, respectively. But before, it is briefly shown how the proposed approach can be used to transfer theoretical results from BDDs to reversible logic.
3.2.3 Theoretical Consideration In the past, first lower and upper bounds for the synthesis of reversible functions containing n variables have been determined. In [MD04b], it has been shown that there exists a reversible function that requires at least (2n / ln 3) + o(2n ) gates (lower bound). Furthermore, the authors proved that every reversible function can be realized with no more than n · 2n gates (upper bound). For a restricted gate library leading to smaller quantum cost and thus only consisting of NOT, CNOT, and two-controlled Toffoli gates (the same as applied for the substitutions proposed here), functions can be synthesized with at most n NOT gates, n2 CNOT gates, and 9 · n · 2n + o(n · 2n ) two-controlled Toffoli gates (according to [SPMH03]). A tighter upper bound of n NOT gates, 2 · n2 + o(n · 2n ) CNOT gates, and 3 · n · 2n + o(n · 2n ) two-controlled Toffoli gates has been proved in [MDM07]. In [PMH08] it has been shown that linear reversible functions are synthesizable with CNOT gates only. Moreover, their algorithm never needs more than Θ(n2 / log n) CNOT gates for any linear function f with n variables.
Table 3.6 Subst. of BDD nodes with complement edge to reversible/quantum circuits
38 3 Synthesis of Reversible Logic
3.2 BDD-based Synthesis
39
Using the synthesis approach proposed in the last sections, reversible circuits for a function f with a size dependent on the number of nodes in the BDD can be constructed. More precisely, let f be a function with n primary inputs which is represented by a BDD containing k nodes.2 Then, the resulting Toffoli circuit consists of at most • k + n circuit lines (since besides the input lines, for each node at most one additional line is added) and • 3 · k gates (since for each node cascades of at most 3 gates are added according to the substitutions of Table 3.4 and Fig. 3.3, respectively). Asymptotically, the resulting reversible circuits are bounded by the BDD size. Since for BDDs many theoretical results exist, using the proposed synthesis approach, these results can be transferred to reversible logic as well. In the following, some results obtained by this observation are sketched. • A BDD representing a single-output function has 2n nodes in the worst case. Thus, each function can be realized in reversible logic with at most 3 · 2n gates (where at most 2 · 2n CNOTs and 2 · 2n Toffoli gates are needed). nodes in the worst case. • A BDD representing a symmetric function has n·(n+1) 2 Thus, each symmetric function can be realized in reversible logic with a quadratic number of gates (more precisely, a quadratic number of CNOTs and a quadratic number of Toffoli gates are needed). • A BDD representing specific functions, like AND, OR, or XOR has a linear size. Thus, there exists a reversible circuit realizing these functions in linear size as well. • A BDD representing an n-bit adder has linear size. Thus, there exists a reversible circuit realizing addition in linear size as well. Further results (e.g. tighter upper bounds for general functions as well as for respective function classes) are also known (see e.g. [Weg00, LL92]). Moreover, in a similar way bounds for quantum circuits can be obtained. However, a detailed analysis of the theoretical results that can be obtained by the BDD-based synthesis is left for future work.
3.2.4 Experimental Results The BDD-based synthesis method together with the suggested improvements has been implemented in C++ on top of the BDD package CUDD [Som01]. In this section, first a case study is given evaluating the effect of the respective BDD optimization techniques on the resulting reversible or quantum circuits. Afterwards, the proposed approach is compared against two previously proposed synthesis methods. 2 For
simplicity, it is assumed that no complement edges are applied.
40
3 Synthesis of Reversible Logic
Benchmarks functions provided by RevLib [WGT+08] (including most of the functions which have been previously used to evaluate existing reversible synthesis approaches) as well as from the LGSynth package [Yan91] (a benchmark suite for evaluating traditional synthesis) have been used. All experiments have been carried out on an AMD Athlon 3500+ with 1 GB of memory. The timeout was set to 500 CPU seconds.
3.2.4.1 Effect of BDD Optimization To investigate the effect of the respective BDD optimization techniques the proposed synthesis approach has been applied to the benchmarks with the respective techniques enabled or disabled. In the following, for each optimization technique (i.e. shared nodes, complement edges, and reordering) the respective results are presented and discussed. Shared Nodes Shared nodes can be enabled or disabled by manipulating the unique table. Then, depending on the respective case, the substitutions of Table 3.4 or additionally of Fig. 3.3 are applied. The results are summarized in Table 3.7. The first two columns give the name of the benchmark (F UNCTION) as well as the number of primary inputs and outputs (PI/PO). Then, the number of resulting circuit lines (n), Toffoli gates (dTof ), or quantum gates (dQua ), as well as the run-time of the synthesis approach (in CPU seconds) is given for the naive approach (denoted by W / O S HARED N ODES) and the approach that exploits shared nodes (denoted by WITH S HARED N ODES). One can clearly conclude that the application of shared nodes leads to better realizations for reversible and quantum logic. Both, the number of lines and the number of gates can be significantly reduced. In particular, for the number of lines this might not be obvious, since additional lines are required to support shared nodes (see Sect. 3.2.2). But due to the fact that shared nodes also decrease the number of terminal nodes (which require additional lines as well), this effect is compensated. Complement Edges Complement edges are supported by the CUDD package and can easily be disabled and enabled. For comparison, circuits from both, BDDs with and BDDs without complement edges (denoted by WITH C OMPL . E DGES and W / O C OMPL . E DGES, respectively), are synthesized. In the latter case, the substitutions shown in Table 3.6 are applied whenever a successor is connected by a complement edge. Shared nodes are also applied, since they make complement edges more beneficial. The results are given in Table 3.8.3 The columns are labeled as described above for Table 3.7. Even if the cascades representing nodes with complement edges are larger in some cases (see Sect. 3.2.2), improvements in the circuit sizes can be observed (see 3 Compared to Table 3.7, also benchmarks are considered for which no result could be determined using the W / O S HARED N ODES approach.
3.2 BDD-based Synthesis
41
Table 3.7 Effect of shared nodes F UNCTION
PI/PO
W/O
n
S HARED N ODES dTof
dQua
WITH
T IME
n
S HARED N ODES T IME
dTof
dQua
7
7
21
<0.01
R EV L IB F UNCTIONS decod24_10
2/4
7
7
21
<0.01
4mod5_8 mini-alu_84
4/1
9
13
36
<0.01
9
13
36
<0.01
4/2
12
21
57
<0.01
11
20
52
<0.01
alu_9
5/1
15
30
73
<0.01
14
29
72
<0.01
rd53_68
5/3
31
85
212
<0.01
20
49
130
<0.01
hwb5_13
5/5
36
105
277
<0.01
32
91
238
<0.01
sym6_63
6/1
23
57
126
0.01
17
34
83
<0.01
hwb6_14
6/6
68
239
618
<0.01
53
167
437
<0.01
rd73_69
7/3
86
301
730
<0.01
38
105
272
<0.01
ham7_29
7/7
75
231
595
<0.01
36
88
224
<0.01
hwb7_15
7/7
136
526
1353
<0.01
84
284
744
<0.01
rd84_70
8/4
194
679
1650
0.01
52
140
373
<0.01
hwb8_64
8/8
277
1132
2903
0.02
129
456
1195
<0.01
sym9_71
9/1
104
325
724
<0.01
35
79
201
<0.01
17
40
98
<0.01
10
19
48
<0.01
LGS YNTH F UNCTIONS xor5
5/1
bw
5/28
125
381
935
0.01
97
286
747
<0.01
9sym
9/1
104
325
724
<0.01
35
79
201
<0.01
e.g. rd84_70, 9sym, or cordic). But in particular for the LGSynth functions, sometimes better circuits result, when complement edges are disabled (see e.g. spla). Here, the larger cascades obviously cannot be compensated by complement edge optimization. In contrast, for quantum circuits in nearly all cases better realizations are obtained with complement edges enabled. A reason for that is that the quantum cascades for nodes with complement edges have the same size as the respective cascades for nodes without complement edges in nearly all cases (see Table 3.4, Fig. 3.3, and Table 3.6, respectively). Thus, the advantage of complement edges (namely the possibility to create smaller BDDs) can be fully exploited without the drawback that the respective gate substitutions become larger. Reordering of BDDs To evaluate the effect of reordering the BDD on the resulting circuit sizes, three techniques are considered: (1) An order given by the occurrences of the primary inputs in the function to be synthesized (denoted by O RIG INAL ), (2) an optimized order achieved by sifting [Rud93] (denoted by S IFTING ), and (3) an exact order [DDG00] which ensures the BDD to be minimal (denoted by E XACT). Again, all created BDDs exploit shared nodes. Furthermore, complement edges are enabled in this evaluation. After applying the synthesis approach,
42
3 Synthesis of Reversible Logic
Table 3.8 Effect of complement edges F UNCTION
PI/PO
W/O
n
C OMPL . E DGES dTof
dQua
WITH
T IME
n
C OMPL . E DGES dTof
dQua
T IME
R EV L IB F UNCTIONS decod24_10
2/4
7
7
21
<0.01
6
11
23
<0.01
4mod5_8
4/1
9
13
36
<0.01
8
16
37
<0.01
mini-alu_84
4/2
11
20
52
<0.01
10
22
49
<0.01
alu_9
5/1
14
29
72
<0.01
11
25
53
<0.01
rd53_68
5/3
20
49
130
<0.01
13
34
75
<0.01
hwb5_13
5/5
32
91
238
<0.01
27
85
201
<0.01
sym6_63
6/1
17
34
83
<0.01
14
29
69
<0.01
hwb6_14
6/6
53
167
437
<0.01
46
157
377
<0.01 <0.01
rd73_69
7/3
38
105
272
<0.01
25
73
162
ham7_29
7/7
36
88
224
<0.01
18
50
82
<0.01
hwb7_15
7/7
84
284
744
<0.01
74
276
665
<0.01
rd84_70
8/4
52
140
373
<0.01
34
104
229
<0.01
hwb8_64
8/8
129
456
1195
<0.01
116
442
1067
<0.01
sym9_71
9/1
35
79
201
<0.01
27
62
153
<0.01
LGS YNTH F UNCTIONS xor5
5/1
10
19
48
<0.01
6
8
8
<0.01
bw
5/28
97
286
747
<0.01
91
317
732
<0.01
ex5p
8/63
276
680
1676
0.02
233
706
1520
0.02
9sym
9/1
35
79
201
<0.01
27
62
153
<0.01
pdc
16/40
648
2074
4844
0.12
631
2109
4803
0.12
spla
16/46
567
1422
3753
0.09
559
1728
3799
0.09
cordic
23/2
76
177
448
0.02
53
109
265
0.02
circuit sizes as summarized in Table 3.9 result. Here again, the columns are labeled as described above. The results show that the order has a significant effect on the circuit size. In particular for the LGSynth functions, the best results are achieved with the exact order. But as a drawback, this requires a longer run-time. Besides that, also in this evaluation, examples can be found, showing that optimization for BDDs not always leads to smaller circuits. Altogether, particularly for larger functions reordering is beneficial. In most of the cases it is thereby sufficient to perform sifting instead of exact reordering, since this lead to results of similar quality but without a notable increase in run-time. For the following evaluations, BDD-based synthesis with shared nodes, complement edges, and sifting has been applied.
3.2 BDD-based Synthesis
43
Table 3.9 Effect of variable ordering F UNCTION PI/PO
O RIGINAL n
S IFTING dQua T IME n
dTof
E XACT dQua T IME n
dTof
dQua T IME
dTof
R EV L IB F UNCTIONS decod24_10 2/4
6
11
23 <0.01
6
11
23 <0.01
6
11
4mod5_8
4/1
8
16
37 <0.01
7
8
18 <0.01
7
8
18 <0.01
mini-alu_84 4/2
10
22
49 <0.01 10
20
43 <0.01 10
20
43 <0.01
alu_9
11
25
53 <0.01
9
22 <0.01
5/1
7
9
rd53_68
5/3
13
34
75 <0.01 13
34
hwb5_13
5/5
27
85
201 <0.01 28
88
sym6_63
6/1
14
29
69 <0.01 14
hwb6_14
6/6
46
157
377 <0.01 46
22 <0.01
7
75 <0.01 13 205
34
0.01 28
88
29
69 <0.01 14
29
159
375 <0.01 46
159
23 <0.01
75 <0.01 205
0.01
69 <0.01 375
0.01
rd73_69
7/3
25
73
162 <0.01 25
73
162 <0.01 25
73
ham7_29
7/7
18
50
82 <0.01 21
61
107 <0.01 21
61
107
0.01
hwb7_15
7/7
74
276
665 <0.01 73
281
653 <0.01 76
278
658
0.01
rd84_70
8/4
34
104
229 <0.01 34
104
229 <0.01 34
104
229 <0.01
hwb8_64
8/8
116
sym9_71
9/1
27
442 1067 <0.01 112 62
153 <0.01 27
449 1047 <0.01 114 62
153 <0.01 27
162 <0.01
440 1051 62
0.03
153 <0.01
LGS YNTH F UNCTIONS xor5
5/1
6
8
bw
5/28
91
317
ex5p
8/63
233
9sym
9/1
27
6
8
732 <0.01 87
8 <0.01
307
706 1520 62
0.02 206
153 <0.01 27
6
8
8 <0.01
693 <0.01 84
8 <0.01
306
667 <0.01
647 1388 62
0.02 206
153 <0.01 27
647 1388 62
0.06
153
0.01
pdc
16/40
631 2109 4803
0.12 619 2080 4781
0.13 619 2087 4850
66.38
spla
16/46
559 1728 3799
0.09 489 1709 4372
0.09 483 1687 4322
86.92
cordic
23/2
0.02 52
0.03 50
53
109
265
101
247
95
237
6.90
3.2.4.2 Comparison to Previous Synthesis Approaches In this section, circuits synthesized by the BDD-based approach are compared to the results generated by (1) the RMRLS approach (described in [GAJ06] using version 0.2 in the default settings) and (2) the RMS approach (based on the concepts of [MDM07] in its most recent version including improved handling of don’t care conditions at the output). Since previous approaches (i.e. RMRLS and RMS) require reversible functions as input, non-reversible functions are embedded into reversible ones (based on the concepts introduced in Sect. 3.1.1). For BDD-based synthesis, the original function description has been used which automatically leads to an embedding. The results are summarized in Table 3.10. The first columns give the name as well as the number of the primary inputs (PI) and primary outputs (PO) of the original function. In the following columns, the number of lines (n), the gate count (dTof ),
PI/PO
2/4 4/1 4/2 5/1 5/3 5/5 6/1 6/6 6/6 7/3 7/7 7/7 8/4 8/8 9/1 9/9 12/12 12/12 13/13 13/13 15/15
F UNCTION
R EV L IB F UNCTIONS decod24_10 4mod5_8 mini-alu_84 alu_9 rd53_68 hwb5_13 sym6_63 mod5adder_66 hwb6_14 rd73_69 hwb7_15 ham7_29 rd84_70 hwb8_64 sym9_71 hwb9_65 cycle10_2_61 plus63mod4096_79 plus127mod8192_78 plus63mod8192_80 ham15_30
4 5 5 5 7 5 7 6 6 9 7 7 11 8 10 9 12 12 13 13 15
11 9 21 9 – – 36 37 – – – – – – – – 26 – – – –
55 25 173 49 – – 777 529 – – – – – – – – 1435 – – – –
497.51 0.86 495.61 122.48 >500.00 >500.00 485.47 494.46 >500.00 >500.00 >500.00 >500.00 >500.00 >500.00 >500.00 >500.00 491.87 >500.00 >500.00 >500.00 >500.00
P REVIOUS A PPROACHES RMRLS [GAJ06] dTof QC T IME n
7 5 36 9 221 42 15 35 100 1344 375 26 124 229 27 2021 41 24 25 28 –
19 9 248 25 2646 214 119 151 740 20779 3378 90 8738 3846 201 23311 1837 4873 9131 9183 – <0.01 <0.01 <0.01 0.01 0.14 0.01 0.13 0.06 0.04 1.93 0.18 0.09 9.92 0.90 3.98 1.45 26.17 17.74 57.16 57.19 >500.00
RMS [MDM07] dTof QC T IME
Table 3.10 Comparison of BDD-based synthesis to previous methods
6 7 10 7 13 28 14 32 46 13 73 21 34 112 27 170 39 23 25 25 45
n
11 8 20 9 34 88 29 96 159 73 281 61 104 449 62 699 78 49 54 53 153
dTof
27 24 60 29 98 276 93 292 507 217 909 141 304 1461 206 2275 202 89 98 97 309
QC
23 18 43 22 75 205 69 213 375 162 653 107 229 1047 153 1620 164 79 86 87 246
dQua
BDD- BASED S YNTHESIS
<0.01 −32 <0.01 −7 <0.01 −130 0.01 −27 <0.01 – 0.01 – <0.01 −708 <0.01 −316 <0.01 – <0.01 – <0.01 – <0.01 – <0.01 – 0.01 – <0.01 – 0.02 – 0.09 −1271 0.08 – 0.21 – 0.20 – 1.25 –
T IME
Δ QC (RMRLS)
4 9 −205 −3 −2571 −9 −50 62 −365 −20617 −2725 17 −8509 −2799 −48 −21691 −1673 −4794 −9045 −9096 –
Δ QC (RMS)
44 3 Synthesis of Reversible Logic
PI/PO
23/2
24/109
39/3
41/35
cordic
cps
apex2
seq
128/28
16/46
spla
ex4p
16/40
pdc
65/65
9/1
9sym
117/88
8/63
ex5p
apex5
5/28
bw
e64
5/1
xor5
LGS YNTH F UNCTIONS
F UNCTION
Table 3.10 (Continued)
∼
∼
∼
∼
∼
∼
∼
∼
∼
∼
∼
∼
∼
∼
∼
∼
∼
∼
∼
∼
–
∼
∼
10
27
6
∼
∼
∼
∼
∼
∼
∼
∼
∼
–
∼
∼
387
∼
∼
∼
∼
∼
∼
∼
∼
∼
>500.00
∼
∼
484.11
P REVIOUS A PPROACHES RMRLS [GAJ06] dTof QC T IME n
∼
∼
∼
∼
∼
∼
∼
∼
∼
27
∼
∼
8
∼
∼
∼
∼
∼
∼
∼
∼
∼
201
∼
∼
68
RMS [MDM07] dTof QC
∼
∼
∼
∼
∼
∼
∼
∼
∼
4.00
∼
∼
0.01
T IME
87
6
510
1147
195
1617
498
930
52
489
619
27
206
n
8
1277
3308
387
5990
1746
2676
101
1709
2080
62
647
307
dTof
8
4009
11292
907
19362
5922
8136
325
5925
6500
206
1843
943
QC
4435
6301
247
4372
4781
153
1388
693
8
dQua
3093
8387
713
14259
BDD- BASED S YNTHESIS
0.03
0.14
0.04
1.14
0.24
0.10
0.02
0.10
0.14
<0.01
0.02
<0.01
<0.01
T IME
–
–
–
–
–
–
–
–
–
–
–
–
−379
Δ QC (RMRLS)
–
–
–
–
–
–
–
–
–
−48
–
–
−60
Δ QC (RMS)
3.2 BDD-based Synthesis 45
46
3 Synthesis of Reversible Logic
the quantum cost (QC), and the synthesis time (T IME) for the respective approaches (i.e. RMRLS, RMS, and the BDD- BASED S YNTHESIS) are reported.4 For BDDbased synthesis, additionally the resulting number of gates (and thus the quantum cost) when directly synthesizing quantum gate circuits is given in the column denoted by dQua . Furthermore, a “∼” denotes that an embedding needed by the previous synthesis approaches could not be created within the given timeout. Finally, the last two columns (Δ QC) give the absolute difference of the quantum cost for the resulting circuits obtained by the BDD-based quantum circuit synthesis and the RMRLS and RMS approach, respectively. As a first result, one can conclude that for large functions to be synthesized it is not always feasible to create a reversible embedding needed by the previous approaches. Moreover, even if this is possible, both RMRLS and RMS need a significant amount of run-time to synthesize a circuit from the embedding. As a consequence, for most of the LGSynth benchmarks no result can be generated within the given timeout. In contrast, the BDD-based approach is able to synthesize circuits for all given functions within a few CPU seconds. Furthermore, although BDD-based synthesis often leads to larger circuits with respect to gate count and number of lines, the resulting quantum cost are significantly lower in most of the cases (except for decod24_10, 4mod5_8, mod5adder_66, and ham7_29). As an example, for plus63mod4096_79 the BDD- BASED S YNTHESIS synthesizes a circuit with twice the number of lines but with two orders of magnitude fewer quantum cost in comparison to RMS. In the best cases (e.g. hwb9_65) a reduction of several thousands in quantum cost is achieved. Note that quantum costs are more important than gate count, since they consider gates with more control lines to be more costly. Thus, even if the total number of circuit lines that have been added by the BDD- BASED S YNTHESIS is higher than by previous approaches, significant improvements in the quantum cost are obtained. Furthermore, reversible logic for functions with more than 100 variables can be automatically synthesized. How to reduce the number of circuit lines is addressed later in Sect. 6.2.
3.3 SyReC: A Reversible Hardware Language Besides synthesis of reversible functions, also how to realize more complex circuits has to be addressed in order to provide an efficient design flow. Thus, synthesis of reversible logic has to reach a level which allows the description of circuits at higher abstractions. For this purpose, programming languages can be exploited. Considering traditional synthesis, approaches using languages like VHDL [LSU89], SystemC [GLMS02], or SystemVerilog [SDF04] have been established to specify and subsequently synthesize circuits. Even if first programming languages are also available in the reversible domain (see e.g. [Abr05, PHW06, YG07]), so far they 4 T IME for BDD- BASED S YNTHESIS includes both, the time to build the BDD as well as to derive the circuit from it.
3.3 SyReC: A Reversible Hardware Language
47
only have been used to design reversible software. Similar approaches for reversible circuit synthesis are still missing. In this section, the programming language SyReC is proposed intended to specify and afterwards to automatically synthesize reversible logic. For this purpose, Janus [YG07]—an existing language designed to specify reversible software—is used as a basis and enriched by new concepts as well as operations aimed to specify reversible circuits. A hierarchical approach is presented that automatically transforms the respective statements and operations of the new programming language into a reversible circuit. Experiments show that complex circuits can be efficiently generated with the help of SyReC. Moreover, a comparison to the BDD-based synthesis approach presented in the previous section shows the advantages of SyReC, if more complex circuits instead of single functions should be synthesized. The remainder of this section is structured as follows: The SyReC programming language as well as the new concepts, operations, and restrictions applied for hardware synthesis are introduced in Sect. 3.3.1. Section 3.3.2 describes the hierarchical synthesis approach and explains in detail how reversible circuits specified in SyReC can be generated. Finally, experimental results and conclusions are given in Sect. 3.3.3.
3.3.1 The SyReC Language As mentioned above, Janus [YG07] is used as a basis for the programming language SyReC to specify reversible systems to be synthesized as circuits. This section briefly reviews the syntax of the Janus language. Afterwards, the new concepts and operations added to address circuit synthesis are introduced.
3.3.1.1 The Software Language Janus Janus is a reversible language that is simple but yet powerful enough to design practical reversible software systems [YG07]. It provides fundamental constructs to define control and data operations while still preserving reversibility. Figure 3.5 shows the syntax of Janus. Each Janus program (denoted by P) consists of variable declarations (denoted by D) and procedure declarations. The variables have non-negative integer values and are denoted by strings. They can be grouped as arrays. New variables are initially assigned to 0. Constants are denoted by c. Each procedure consists of a name (id) and a sequence of statements (denoted by S) including operations, reversible conditionals, reversible loops, as well as call and uncall of procedures (lines 4 to 7 in Fig. 3.5). Variables within statements are denoted by V . In the following, it is distinguish between reversible assignment operations (denoted by ⊕) and (not necessarily reversible) binary operations (denoted by ). The former ones assign values to a variable on the left-hand side. Therefore, the respective variable must not appear in the expression on the right-hand side. Furthermore,
48
3 Synthesis of Reversible Logic
only a restricted set of assignment operations exists, namely increase (+ =), decrease (− =), and bit-wise XOR (ˆ =), since they preserve the reversibility (i.e. it is possible to compute these operations in both directions). In particular, the bit-wise XOR is of interest because aˆ = b is equal to an assignment a = b if a is equal to 0. In contrast, binary operations, i.e. arithmetic (+, ∗, /, %, ∗/), bit-wise (&, |, ˆ), logical (&&, ||), and relational (<, >, =, ! =, <=, >=) operations, may not be reversible. Thus, they can only be used in right-hand expressions which preserve (i.e. do not modify) the values of the respective inputs. In doing so, all computations remain reversible since the input values can be applied to revert any operation. For example, to specify a multiplication (i.e. a ∗ b) in Janus, a new free variable c must be introduced which is used to store the product (i.e. cˆ = a ∗ b is applied). In comparison, to common (irreversible) programming languages this forbids statements like a = a ∗ b. Having this as basis, Janus can be used to specify reversible programs and execute them in a reversible manner (i.e. forward and backward).
3.3.1.2 The Hardware Language SyReC In the following, the programming language SyReC for synthesis of reversible circuits is described. Janus is thereby used as a basis and enriched by further concepts (e.g. declaring circuit signals of different bit-width) and operations (e.g. bit-access and shifts). Besides that, some restrictions are applied (e.g. dynamic loops are forbidden in hardware). Incorporating all these aspects, a syntax of a programming language for reversible circuit synthesis as depicted in Fig. 3.6 results. More precisely, the following extensions and restrictions have been applied: • The declaration of variables has been extended so that the designer can declare variables with different bit-widths (line 2). • Arrays are not allowed. • Operators to access single bits (x.N), a range of bits (x.N:N), as well as the size (#V) of a variable, respectively, have been added (line 3 and line 4). • Since loops must be completely unrolled during synthesis, the number of iterations has to be available before compilation. That is, dynamic loops (defined by expressions) are not allowed (line 7). • Macros for the SWAP operation (<=>) (line 5) as well as for the for-loop statement (line 8) have been added.5 • Further operations used in hardware design (e.g. shifts <) have been added (line 10 and line 14). Example 3.3 Figure 3.7 shows a simple Arithmetic Logic Unit (ALU) illustrating the core concept of the resulting hardware programming language. The basic arithmetic operations can be thereby applied directly. Furthermore, control variables can be defined with a lower bit-width than data variables. 5 These extensions are not necessarily needed (i.e. they can also be expressed by the existing operations), but they allow a more intuitive programming of reversible circuits.
3.3 SyReC: A Reversible Hardware Language
49
Fig. 3.5 Syntax of the software language Janus
(1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (11)
P ::= D∗ (procedure id S+ )+ D ::= x | x[c] V ::= x | x[E] S ::= V ⊕ = E | if E then S else S fi E | from E do S loop S until E | call id | uncall id | skip E ::= c | V | (E E) ⊕ ::= + | − | ˆ ::= ⊕ | ∗ | / | % | ∗/ | & | | | && | | <|>|=|!=|<=|>=
Fig. 3.6 Syntax of the hardware language SyReC
(1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (11) (12) (13) (14)
P ::= D∗ (procedure id S+ )+ D ::= x | x ( c ) V ::= x | x.N:N | x.N N ::= c | #V S ::= V < => V | V ⊕ = E | if E then S else S fi E | from N do S loop S until N | for N do S until N | call id | uncall id | skip E ::= N | V | (E E) | (E < N) ⊕ ::= + | − | ˆ ::= ⊕ | ∗ | / | % | ∗/ | & | | | && | | < | > | = | ! = | <= | >= < ::= << | >>
Fig. 3.7 SyReC example: ALU
op ( 2 ) x0 x1 x2 procedure a l u i f ( op = 0 ) t h e n x0 ^= ( x1 + x2 ) else i f ( op = 1 ) t h e n x0 ^= ( x1 − x2 ) else i f ( op = 2 ) t h e n x0 ^= ( x1 ∗ x2 ) else x0 ^= ( x1 ^ x2 ) f i ( op = 2 ) f i ( op = 1 ) f i ( op = 0 )
50
3 Synthesis of Reversible Logic
In contrast to previous approaches, this allows a much easier specification of (complex) reversible circuits. Having this, the next section describes how circuits can be synthesized from this representation.
3.3.2 Synthesis of the Circuits Using the language introduced above it is possible to specify reversible circuits on a higher level. As demonstrated by Example 3.3 this particularly allows to design complex circuits in an easier way than e.g. by truth tables or decision diagrams. Nevertheless, the specified circuits still need to be synthesized. To this end, a hierarchical synthesis method is proposed that uses existing realizations of the individual operations (i.e. building blocks) and combines them so that the desired circuit results. More precisely, the approach (1) traverses the whole program and (2) adds cascades of reversible gates to the circuit to be synthesized for each statement or expression, respectively. In the following, the individual mappings of the operations and expressions to the respective reversible cascades are described.
3.3.2.1 Reversible Assignment Operations As introduced in Sect. 3.3.1, reversible assignment operations include those which are reversible even if they assign a new value to the variable on the left-hand side of a statement. In the following, the notation as depicted in Fig. 3.8(a) is used to denote such an operation in a circuit structure.6 Solid lines represent the variable(s) on the right-hand side of the operation, i.e. the variable(s) whose values are preserved. The simplest reversible assignment operation is the bit-wise XOR (e.g. x1 ˆ = x2 ). For Boolean variables, this operation can be synthesized by a single Toffoli gate as shown in Fig. 3.8(b). If variables with a bit-width greater than 1 are applied, then a Toffoli gate has to be analogously applied for each bit. To synthesize the increase operation (e.g. a+ = b), a modified addition network is added. In the past, several realizations of addition in reversible logic have been investigated. In particular, it is well known that the minimal realization of a onebit adder consists of four Toffoli gates (see e.g. [WGT+08]). Thus, cascading the required number of one-bit adders is a possible realization. But since every onebit adder also requires one constant input, this is a very poor solution with respect to circuit lines. In contrast, heuristic realizations exist that require a fewer number of additional lines (see e.g. [TK05]). Here, a realization with only one additional line (which additionally can be reused for any further addition operation) is used. A cascade showing this realization for a 3-bit addition is depicted in Fig. 3.8(c). Nevertheless, any other adder realization can be applied as well. 6 Figure 3.8(a) shows the notation for a single bit operation. For larger bit-widths the notation is extended accordingly.
3.3 SyReC: A Reversible Hardware Language
51
Fig. 3.8 Realization of reversible assignment operations
Fig. 3.9 Realization of binary operations
Finally, the mapping for the decrease operation is left (e.g. a− = b). Here, also the realization from Fig. 3.8(c) is applied, which is fed with a negated variable value.
3.3.2.2 Binary Operations Binary operations include operations that are not necessarily reversible so that its inputs have to be preserved to allow a (reversible) computation in both directions. To denote such operations, in the following the notation depicted in Fig. 3.9(a) is used. Again, solid lines represent the variable(s) whose values are preserved (i.e. in this case the input variables). Synthesis of irreversible functions in reversible logic is not new so that for most of the respective operations already a reversible circuit realization exists. Additional
52
3 Synthesis of Reversible Logic
lines with constant inputs are thereby applied to make an irreversible function reversible (see e.g. Sect. 3.1.1). As an example, Fig. 3.9(b) shows a reversible cascade that realizes an AND operation. As can be seen, this requires one additional circuit line with a constant input 0. Similar mappings exist for all other operations. However, since binary operations can be applied together with reversible assignment operations (e.g. cˆ = a&b), sometimes a more compact realization is possible. More precisely, additional (constant) circuit lines can be saved (at least for some operations), if the result of a binary operation is applied to a reversible assignment operation. As an example, Fig. 3.9(c) shows the realization for cˆ = a&b where no constant input is needed but the circuit line representing c is used instead. However, such a “combination” is not possible for all operations. As an example, Fig. 3.9(d) shows a two-bit addition whose result is applied to a bit-wise XOR, i.e. cˆ = a + b. Here, removing the constant lines and directly applying the XOR operation on the lines representing c would lead to a wrong result. This is because intermediate results are stored at the lines representing the sum. Since these values are reused later, performing the XOR operation “in parallel” would destroy the result. Thus, to have a combined realization of a bit-wise XOR and an addition, a concrete embedding for this case must be generated. Since finding and synthesizing respective embeddings for all affected operations and combinations, respectively, is a non-trivial task, a more detailed consideration of this aspect is left for future work. So far, constant lines are applied to realize the desired functionality. In this sense, most of the binary operations (in particular the bit-wise, logical, and relational operations as well as the addition) are synthesized. Besides that, the realization of the multiplication is of interest. A couple of possible ways are described in [OWDD10]. Figure 3.9(e) briefly shows how multiplication is realized by the proposed synthesis method. As can be seen, partial products are applied. Considering one of the factors a, each time a respective bit of this factor (denoted by ai ) is equal to 1, the respective partial product is added to the product. This allows to reuse the increase realization introduced in the previous section.
3.3.2.3 Conditional Statements, Loops, Call/Uncall Finally, the realization of control operations as reversible cascades is considered. Loops and procedure calls/uncalls can be realized in a straightforward way. More precisely, loops are realized by simple cascading (i.e. unrolling) the respective statements within a loop block for each iteration. Since the number of iterations must be available before synthesis (see Sect. 3.3.1), this results in a finite number of statements which is subsequently processed. Call and uncall of procedures are handled similarly. Here, the respective statements in the procedures are cascaded together. To realize conditional statements (e.g. the one shown in Fig. 3.10(a)), two variants are proposed. Figure 3.10(b) shows the first one, which is realized in three steps: 1. All variables in the then- or else-block, respectively, which potentially are assigned with a new value (i.e. that are on the left-hand side of a reversible assign-
3.3 SyReC: A Reversible Hardware Language
53
Fig. 3.10 Realization of an if-statement
ment operation) are duplicated. This respectively requires an additional circuit line with constant input 0. 2. The statements in the respective blocks are mapped to reversible cascades. The duplications introduced in the last step are thereby applied to intermediately store the results of the then-block and the original values of the variables in the elseblock, respectively. 3. Depending on the result of the if-statement e, the respective values of the duplicated lines and the original lines are swapped. More precisely, in the example of Fig. 3.10(a) the value of a is swapped with its (newly assigned) duplication iff e evaluates to 1. Analogously, iff e evaluates to 0 the (newly assigned) value of c is passed through. The second realization of a conditional statement is depicted in Fig. 3.10(c). In contrast to the previous one, here no duplications (and therewith no additional circuit lines) are required. Instead, control lines are added to all gates in the realization of the respective then- and else-block. Thus, the operations are computed iff the expression e is assigned to 1 or 0, respectively. A NOT gate (i.e. a Toffoli without control lines) is thereby used to flip the value of e so that the gates of the else block can be “controlled” as well. Having both realizations, it is up to the designer which one should be used during synthesis. Using the first realization leads to additional circuit lines (particularly in quantum logic a restricted resource). This is not in case in the second realization; however, here due to the additional control lines both the quantum cost and the transistor cost of the circuit significantly increase. Besides other aspects, this is also evaluated in the experiments in the next section.
3.3.3 Experimental Results The proposed synthesis approach for the programming language SyReC has been implemented in C++. In this section, experimental results obtained by this approach are provided. In particular, the different realizations of conditional statements are evaluated in more detail. Furthermore, the results obtained by the pro-
54
3 Synthesis of Reversible Logic
posed approach are compared to the BDD-based synthesis method introduced in Sect. 3.2. As benchmarks a couple of programs are used including a simple arithmetic logic unit (denoted by alu; see also Fig. 3.7), a program computing the average of 8 or 16 values (denoted by avg8 and avg16), a logic unit applying bit-wise operations instead of arithmetic (denoted by lu), as well as an arbiter with 8 clients (denoted by arb8). Thus, results obtained by programs including arithmetic (alu, avg8, and avg16) as well as bit-wise operations (lu and arb8) have been evaluated. All experiments have been carried out on an AMD DualCore Athlon 3 GHz machine with 32 GB of main memory. The time-out was set to 500 CPU seconds. In a first evaluation the effect of different if-statement realizations was considered in detail. The results are presented in Table 3.11(a). The first column gives the name of the benchmark followed by the applied bit-width of data variables (denoted by BW) and the resulting number of primary inputs (denoted by PI). The following columns give the number of constant input lines (CI), the number of gates (d), the quantum cost (QC), and the transistor cost (TC) of the circuits obtained using the if-realization with additional circuit lines (denoted by IF - STM . W / ADD . LINES) or without additional circuit lines (denoted by IF - STM . W / O ADD . LINES), respectively. Finally, the run-time is reported for both approaches in column T IME. The results confirm the discussion from the last section. If additional circuit lines are applied, the respective cost can be significantly reduced. In comparison to the realization without additional circuit lines for if-statements, approx. 80% of the quantum costs and at least for alu and lu more than 50% of the transistor costs can be saved (this does not hold for the avg benchmarks since they do not include ifstatements). In contrast, this leads to a significant increase in the number of constant inputs. Finally, the results are compared to the BDD-based synthesis approach. Here, a function given as binary decision diagram is used as input. Thus, the circuits obtained by SyReC are extracted as BDDs and re-synthesized based on the concepts introduced in Sect. 3.2.7 The results are given in Table 3.11(b) using the same denotation as described above. As can be clearly seen, the proposed approach outperforms the BDD-based synthesis in all objectives: Circuits with significantly less number of gates, quantum cost, and transistor cost, respectively, are synthesized in much less run-time (only the small arb8 is an exception). Moreover, in particular for the benchmarks including arithmetic (i.e. alu and awg) for large bit-widths no circuit can be synthesized within the given time-out. This can be explained by the fact, that in particular for the multiplication no efficient representation as BDD exists. Thus, for these examples the BDD-based approach suffers from memory explosion. Altogether, SyReC allows the specification of complex circuits that are hard to describe in terms of a decision diagram or truth table, respectively. Afterwards, the specified circuits can efficiently be synthesized. 7A
similar comparison to further work (e.g. [GAJ06, MDM07]) was not possible since due to memory limitations the respective benchmarks cannot be represented in terms of truth tables which is required by these approaches.
3.3 SyReC: A Reversible Hardware Language
55
Table 3.11 Experimental results (a) Applying the programming language SyReC B ENCH BW
PI
IF - STM . W / ADD . LINES
CI
453 2069
T IME CI
5840 0.03 s
QC
d
41
TC
T IME
8
alu
16
50 121 1345 7377 19856 0.03 s
alu
32
98 233 4473 27785 72464 0.03 s 137 4284 155677 166136 0.03 s
8
65
TC
alu
avg8
26
QC
d
IF - STM . W / O ADD . LINES
408
11125
13208 0.02 s
73 1252
40749
45240 0.03 s
72
11
405
885
4200 0.01 s
11
405
885
avg8
16 144
19
861 1853
8872 0.01 s
19
861
1853
8872 0.01 s
avg8
32 288
35 1773 3789 18216 0.01 s
35 1773
3789
18216 0.01 s
12
12
avg16
8 136
754
1654
7832 0.01 s
avg16
16 272
20 1602 3462 16536 0.01 s
20 1602
3462
16536 0.01 s
avg16
32 544
36 3298 7078 33944 0.01 s
36 3298
7078
33944 0.01 s
64
164
392
1328 0.02 s
40
119
1960
2960 0.02 s
744
2544 0.02 s
72
215
3768
5584 0.02 s
4976 0.03 s 136
407
7384
10832 0.02 s
24
746
800 0.44 s
lu
8
lu
16
50 120
308
lu
32
98 232
596 1448
arb8
26
754 1654
1
16
37
80
296
7832 0.01 s
4200 0.01 s
640 0.45 s
1
(b) BDD-based synthesis B ENCH
BW
PI
BDD-based synthesis CI
QC
d
TC
T IME
alu
8
26
768
3560
11196
70792
0.06 s
alu
16
50
541099
2842702
9380494
57530752
283.63 s
alu
32
98
–
–
–
–
>500.00 s
avg8
8
72
2933
10449
36581
217240
4.61 s
avg8
16
144
–
–
–
–
>500.00 s
avg8
32
288
–
–
–
–
>500.00 s
avg16
8
136
7410
25454
89938
532424
9.61 s
avg16
16
272
–
–
–
–
>500.00 s
avg16
32
544
–
–
–
–
>500.00 s
lu
8
26
111
331
823
5928
0.01 s
lu
16
50
215
651
1623
11688
0.03 s
lu
32
98
423
1291
3223
23208
0.08 s
1
16
15
49
101
824
0.01 s
arb8
56
3 Synthesis of Reversible Logic
3.4 Summary and Future Work Having automated synthesis methods is crucial in the design of reversible or quantum circuits. In this chapter, the current synthesis steps (including embedding and actual synthesis) have been described. Even if only a selected approach has been considered in detail, it was illustrated and discussed that synthesis of reversible logic currently is still at the beginning. Most of the existing methods are not able to synthesize large Boolean functions—not to mention complex reversible systems. One contribution towards synthesis of significantly larger circuits was made in the second part of this chapter. Here, BDDs representing the function to be synthesized are constructed whose nodes afterwards are substituted with a cascade of Toffoli or quantum gates, respectively. While previous approaches are only able to handle functions with up to 30 variables at high run-time, the BDD-based approach can synthesize circuits for functions with more than hundred variables in just a few CPU seconds. Furthermore, with respect to quantum cost (i.e. number of quantum gates), significantly smaller realizations are obtained. Due to these promising results, BDD-based synthesis should be subject to further research. In particular, a detailed analysis of the theoretical results that can be obtained by the BDD method is left. Section 3.2.3 gave a first sketch. However, BDDs are very well understood so that for sure many more results can be transferred. Furthermore, it would be of interest to evaluate the effect of the proposed approach if adjusted cost functions for reordering as well as other decompositions (e.g. positive Davio or negative Davio) are applied. Besides that, synthesis of reversible logic should reach the “next” level, i.e. the system level. Therefore, reversible hardware programming languages are needed. SyReC, as introduced in the third part of this chapter, provides a first approach in this direction. Using this language in combination with the proposed hierarchical synthesis approach enables synthesis of more complex reversible circuits for the first time. Nevertheless, the circuits resulted from both, BDD-based as well as SyReCbased synthesis, still require a notable amount of additional circuit lines. Depending on the technology this might be a drawback (e.g. for quantum systems where the number of lines or qubits, respectively, is limited). Thus, how to reduce these number of lines in a circuit is an important question. Section 6.2 introduces a postprocess approach which addresses this issue. Besides that, finding embeddings leading to fewer additional circuit lines (e.g. for the binary operations in SyReC) is an important task for future work.
Chapter 4
Exact Synthesis of Reversible Logic
In contrast to the heuristic approaches introduced in the last chapter for synthesis of reversible logic, exact methods determine a minimal solution, i.e. a circuit with a minimal number of gates or quantum cost, respectively. Ensuring minimality often causes an enormous computational overhead and thus exact approaches are only applicable to relatively small functions. Nevertheless, it is worth to consider exact methods, since they • allow finding smaller circuits than the currently best known realizations, • allow the evaluation of the quality of heuristic approaches, and • allow the computation of minimal circuits as building blocks for larger circuits. For example, improving heuristic results by 10% is significant, if this leads to optimal results, but marginal if the generated results are still factors away from the optimum. Conclusions like this are only possible, if the optimum is available. Another aspect is the computation of building blocks that can be reused to synthesize larger designs. For example, the substitutions used in the last chapter for the BDDbased synthesis have been generated using exact approaches. However, only very little research has been done in exact synthesis of reversible logic so far. A method based on a depth-first traversal with iterative deepening that uses circuit equivalences to rewrite a limited set of gates has been presented in [SPMH03]. The authors of [YSHP05] introduce an exact algorithm based on group theory. But for both approaches, only results for functions with up to three variables are reported. Furthermore, in [HSY+06] another exact synthesis method based on a reachability analysis has been proposed which is geared towards quantum gates. However, also here only functions with three and a couple of functions with four variables can be handled in a significant amount of run-time. This chapter proposes methods based on Boolean satisfiability (SAT) that allow a faster exact synthesis and that is applicable for functions with up to six variables. The general idea is as follows: The synthesis problem is formulated as a sequence of decision problems. Then, each decision problem is encoded as a SAT instance and checked for satisfiability using an off-the-shelf SAT solver. If the instance is unsatisfiable, then no realization with d gates exists and a check for another d value R. Wille, R. Drechsler, Towards a Design Flow for Reversible Logic, DOI 10.1007/978-90-481-9579-4_4, © Springer Science+Business Media B.V. 2010
57
58
4 Exact Synthesis of Reversible Logic
is performed. Otherwise, the circuit can be obtained from the satisfying assignment. Minimality is ensured by iteratively increasing d starting with d = 1. In the following, the main flow and the respective SAT encodings for Toffoli circuit synthesis [GCDD07, GWDD09a] as well as for quantum logic synthesis [GWDD08, GWDD09b] are introduced in detail in Sects. 4.1 and 4.2, respectively. Since nowadays very powerful techniques for solving SAT instances exist (see Sect. 2.3), already this enables efficient exact synthesis of reversible functions. However, further improvements are possible, if (1) the problem is formulated and solved on the SMT level [WGSD08], (2) additional knowledge provided by the dedicated solving engine SWORD is exploited [WG07, GWDD09a], or (3) quantified Boolean satisfiability is used [WLDG08]. The respective encodings and methods are described in Sect. 4.3. The last (and most efficient) method also has been applied and evaluated to synthesize reversible circuits including Fredkin and Peres gates. Finally, the chapter is concluded and future work is sketched in Sect. 4.4.
4.1 Main Flow In this section, the main concepts of the exact synthesis algorithm for reversible and quantum logic are presented. The basic idea is as follows: Given a reversible function f to be synthesized. Then, the exact synthesis of f is formulated as a sequence of decision problems. In each iteration, it is checked if for the reversible function f and a natural number d a circuit with exactly d gates exists. Here, options for specifying and solving the decision problem as well as finding the optimal value for d exist. The decision problem is encoded and solved by using SAT techniques which is described in the next section in detail. To find the optimal value for d, i.e. to determine a d where the resulting circuit has the minimal number of gates, a possible approach is to start searching a solution with d = 1. If there is no solution, i.e. the decision problem returns false, the number of gates (i.e. d) is incremented until one of the remaining decision problems becomes true. Following this procedure, minimality is ensured. Obviously, it is also possible to choose another technique to reach the optimal d. For example, upper or lower bounds can be exploited. However, for the exact synthesis problem at hand the following observation holds that is first illustrated with an example and afterwards formulated in a lemma. Example 4.1 Consider the reversible function in Fig. 4.1(a). For this function two Toffoli circuits are shown in Fig. 4.1(b). By exhaustive enumeration it has been proven that, even if there are realizations including d = 2 gates and d = 4 gates, no realization with d = 3 gates exist. Hence, if a realization with d gates has been found, minimality cannot be shown by only proving that there is no realization with d − 1 gates. However, for Toffoli circuits it is sufficient to prove that there are no realizations with d − 1 and d − 2 gates as the following lemma shows:
4.1 Main Flow
59
Fig. 4.1 Function with circuits including d = 2/4, but not d = 3 gates
Fig. 4.2 Extension of a circuit with d gates to a circuit with d + 2 gates
Fig. 4.3 Gate equivalences
Lemma 4.1 Let f : Bn → Bn be a reversible function to be synthesized. A Toffoli circuit including d gates is minimal with respect to the number of gates, if no realization with d − 1 gates and no realization with d − 2 gates exists. Proof Assuming that for a reversible function f a realization with d gates and a smaller realization with d − r gates exist (r > 0). Then, as shown in Fig. 4.2, the smaller realization can be extended by two additional NOT gates so that the resulting circuit still realizes f . By cascading the respective NOT gates, it follows that there are realizations with d − r + 2 · s gates as well (s > 0). Thus, if there is a realization with d − r gates, there has to be at least one realization with d − 1 or with d − 2 gates. If quantum gate circuits are considered this observation can be applied as well. Moreover, if at least a CNOT gate, a V gate, or a V+ gate occurs, a somewhat tighter extension is possible. Then, each of these gates can be “extended” as depicted in Fig. 4.3. This leads to valid realizations with costs d + r for any r ∈ N. As a result, it is sufficient to check for a realization with d − 1 gates to prove that d is minimal. Thus, the minimal d can be approached by two methods: (1) Start with d = 1 and iteratively increment d until a realization is found or (2) determine a value for d (e.g. by heuristics or bounds) and non-iteratively modify d until a minimal realization (approved by Lemma 4.1) is found. In this context, non-iteratively means that if there exists a circuit with d gates, then it is tried to find a better realization with only d < d gates; otherwise, it is tried to find a circuit with d > d gates. However, for the considered exact synthesis problem an iterative approach is chosen due to the complexity in solving the respective problem instances for large values of d. To illustrate this, Table 4.1 shows the results (R ES) as well as the run-time
60 Table 4.1 Iterative approach vs. non-iterative approach for mod5d1
4 Exact Synthesis of Reversible Logic d
1 2 3 4 5 6 7 total
Fig. 4.4 Main flow of exact synthesis algorithm
I TERATIVE R ES
T IME
B EST CASE NON - IT. R ES T IME
UNSAT UNSAT UNSAT UNSAT UNSAT UNSAT SAT
0.23 1.92 16.68 36.62 194.24 1625.88 218.56
– – – – UNSAT UNSAT SAT
2094.13
(1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (11) (12) (13) (14) (15)
– – – – 194.24 1625.88 218.56 2038.68
exactSynthesis(f : Bn → Bn ) // f is given in terms of a truth table found = false; d = 1; while (found == false) do inst = encodeProblem(f, d); res = callSolver(inst); if (res == satisfiable) // f is synthesizable with costs d A = getAssignment(); extractCircuitFromAssignment(A); found = true; else // f is not synthesizable with costs d d = d + 1;
(T IME, in CPU seconds) of the respective checks that have been performed to synthesize an optimal Toffoli circuit for the function mod5d1.1 The minimal circuit for this function includes d = 7 gates. Thus, using the iterative approach seven checks are performed in total. In contrast, assuming the best case for the non-iterative approach (i.e. the minimal depth d = 7 is determined just at the beginning and additionally the two checks for d = 6 and d = 5 are performed) only three checks are necessary. However, since the run-time needed for the first checks of the iterative approach are small, the total run-time for both approaches differs only slightly (less than 3%). Hence, the non-iterative approach for reaching the minimal d is not feasible in general. This particularly holds, since this approach naturally requires checks with a d greater than the minimal d (that obviously are harder). As a result, an iterative approach as shown in Fig. 4.4 is used for exact synthesis of reversible logic. The input is the truth table of the reversible function f to 1 Therefore, the SAT-based encoding described in the next section has been used. However, the same behavior was observed for other encodings and functions, respectively.
4.2 SAT-based Exact Synthesis
61
be synthesized. The algorithm tries to find a circuit representation for f with one gate only, i.e. d is initialized to 1 and a respective SAT instance is created. If no realization with d gates exists, d is incremented. This procedure is repeated until a realization is found. The respective checks are thereby performed by 1. encoding the synthesis problem as an instance of Boolean satisfiability inst (line 6) and 2. checking the instance for satisfiability using an off-the-shelf solver (line 7). If there exists a satisfying assignment for inst a circuit representing f has been found. This circuit is extracted from the assignment of the encoding given by the solver. If inst is unsatisfiable, it has been proven that no realization for f with d gates exists. By increasing d iteratively from d = 1 minimality is ensured. Using this as the main flow, the next sections introduce concrete encodings for Toffoli and quantum circuit synthesis, respectively.
4.2 SAT-based Exact Synthesis Having the main flow as a basis, the open question still is how to encode the decision problem “Is there a circuit with exactly d gates that realizes the given reversible function f ?” as a SAT instance. In this section, the concrete SAT formulation as well as first results obtained with it are presented. Section 4.2.1 addresses Toffoli circuit synthesis, while Sect. 4.2.2 is about quantum circuit synthesis, respectively. These encodings allow an efficient handling of the embedding problem for irreversible functions (see Sect. 3.1.1) which is considered in Sect. 4.2.3 in more detail. Finally, experimental results are given in Sect. 4.2.4.
4.2.1 Encoding for Toffoli Circuits The synthesis problem for Toffoli circuits is encoded so that the resulting instance is satisfiable iff a circuit with d gates realizing a given function f exists; otherwise the instance must be unsatisfiable. To this end, Boolean variables (for brevity denoted by vectors in the following) and constraints are used as described in the following. First, the vectors defining the type of a Toffoli gate at an arbitrary depth k are introduced:2 2 The Toffoli gates in a circuit are enumerated from left to right (starting from 0). Furthermore, the term depth is used to refer to the respective position of a Toffoli gate in this enumeration.
62
4 Exact Synthesis of Reversible Logic
Fig. 4.5 Representation of Toffoli gates by assignments to tk and ck
Definition 4.1 Let f : Bn → Bn be a reversible function to be synthesized as a circuit with d gates. Then, k . . . t1k ) with 0 ≤ k < d is a Boolean vector defining the position of • tk = (tlog 2 n the target line for the Toffoli gate at depth k. More precisely, tk is a binary encoding of a natural number t k ∈ {0, . . . , n − 1} that indicates this target line. k ck k • ck = (cn−1 n−2 . . . c1 ) with 0 ≤ k < d is a Boolean vector defining the control lines of the Toffoli gate at depth k. More precisely, assigning clk = 1 with (1 ≤ l < n) means that line (t k + l) mod n becomes a control line of the Toffoli gate at depth k.
Remark 4.1 In total there are n · 2n−1 different types of Toffoli gates for a reversible function with n variables. This holds, since a Toffoli gate has exactly one target line resulting in n − 1 lines that are left as possible control lines. Thus, there are n lines possible for placing a target line and 2n−1 combinations for control lines, respectively. Example 4.2 Figure 4.5 shows all 3 · 23−1 = 12 possible types of Toffoli gates for a circuit with n = 3 lines. For each gate its assignments to the vectors tk and ck are also given. For example, the assignments tk = (01) and ck = (01) state that line [01]2 = 1 is the target line. Furthermore, because c1 is assigned to 1, line (1 + 1) mod 3 = 2 becomes a control line. In contrast, because c2 is assigned to 0, line (1 + 2) mod 3 = 0 does not become a control line. Furthermore variables representing the inputs and outputs as well as the internal signals of the circuit to be synthesized are defined:
4.2 SAT-based Exact Synthesis
63
Fig. 4.6 SAT formulation for Toffoli circuit synthesis with n = 4 and d = 4
Definition 4.2 Let f : Bn → Bn be a reversible function to be synthesized as a k k ) with 0 ≤ i < 2n and 0 ≤ k ≤ d is . . . xi0 circuit with d gates. Then, xki = (xi(n−1) a Boolean vector representing the input (for k = 0), the output (for k = d), or the internal variables (for 1 ≤ k ≤ d − 1), respectively, of the circuit to be synthesized for each truth table line i of f . So, the left side of a truth table line i corresponds to the vector x0i , while the right side corresponds to the vector xdi , respectively. Example 4.3 Figure 4.6 shows the variables needed to formulate the synthesis problem for an (embedded) adder function3 with n = 4 variables and depth d = 4. The first row gives the variables for the first truth table line, the second row the variables for the second truth table line, and so on. Thus, for each of the 24 = 16 lines in the truth table, n = 4 circuit lines with the respective vectors for input, output, and internal variables are considered (i.e. overall 4 · 16 = 64 lines are considered). The positions for the Toffoli gates to be synthesized are marked by dashed rectangles. For each depth, all possible types of Toffoli gates can be defined by assigning the respective values to tk and ck .
3 In
the example, the adder from Table 3.2 on p. 29 is used.
64
4 Exact Synthesis of Reversible Logic
Using these variables, the synthesis problem for a reversible function f with d Toffoli gates can be formulated as follows: Is there an assignment to all variables of the vectors tk and ck such that for each line i, x0i is equal to the left side of the truth table, while xdi is equal to the corresponding right side? This is encoded by the conjunction of the following three constraints: 1. The input/output constraints set the input and output of the truth table given by the function f to the respective variables x0i and xdi (see also left-hand and righthand side of Fig. 4.6), i.e. n −1 2
[x0i ]2 = i ∧ [xdi ]2 = f (i).
i=0
2. For each gate to be synthesized at depth k, functional constraints are added so that—depending on the assignments to tk and ck as well as on the input xki of the is computed, i.e. kth gate for truth table line i—the respective gate output xk+1 i n −1 d−1 2
xk+1 = t (xki , tk , ck ). i
i=0 k=0
The function t (xki , tk , ck ) covers the functionality of a Toffoli gate with target line t k = [tk ]2 and the control lines defined by ck . As an example, consider tk = (01) and ck = (100), i.e. with c3k = 1. This assignment states that the Toffoli gate at depth k has line t k = [01]2 = 1 as target line and line (t k + l) mod n = (1 + 3)
mod 4
=0 as single control line. For this case, constraints tk = (01) ∧ ck = (100) ⇒
k+1 k xi0 = xi0 k+1 k ⊕ xk ∧ xi1 = xi1 i0 k+1 k ∧ xi2 = xi2 k+1 k ∧ xi3 = xi3
are added for each truth table line i of a function with n = 4 variables. That means, the values of the circuit lines 0, 2, and 3 are passed through, while the output value of line 1 becomes inverted, if line 0 is assigned to 1. Similar constraints are added for all remaining cases.
4.2 SAT-based Exact Synthesis
65
3. Finally, exclusion constraints ensure that illegal assignments to tk are excluded, since not all values of tk are necessary to enumerate all possible target lines, i.e. d−1
[tk ]2 < n.
k=0
For example, for a circuit consisting of n = 3 lines the target line is represented by two variables tk = (t2 t1 ) as shown in Fig. 4.5. Here, the assignment tk = (11) has to be excluded, since line [11]2 = 3 does not exist. As a result, a formulation has been constructed which is satisfiable, if there is a valid assignment to tk and ck so that for all truth table lines the desired inputoutput mapping is achieved. Then, the concrete Toffoli gates can be obtained by the assignments to tk and ck as depicted in Fig. 4.5. If there is no such assignment (i.e. the instance is unsatisfiable), then it has been proven that no circuit representing the function with d gates exists. As a last step, the proposed encoding has to be transformed from bit-vector logic into a Conjunctive Normal Form (CNF)—the standard input format for SAT solvers (see Sect. 2.3.1). This is a well understood process that can be done in time and space linear in the size of the original formulation [Tse68]. A possible way is to define methods for clause generation of simple logic functions like AND, OR, etc. and extending this scheme for more complex logic like implications or comparisons. Then, in particular the functional constraints can be mapped to CNF. The assignments of the input/output constraints can be applied by using unit clauses. Finally, the exclusion constraints can be expressed by explicitly enumerating all values that are not allowed in terms of a blocking clause [McM02]. Having the formulation in CNF, the satisfiability of the instance (as well as the satisfying assignments) can be efficiently determined.
4.2.2 Encoding for Quantum Circuits To synthesize quantum circuits, an encoding similar to the one introduced in the last section is used. However, since quantum circuits may have V gates and V+ gates, circuit lines (or qubits, respectively) may not only be assigned to Boolean 0 and Boolean 1, but also to V0 and V1 (see Sect. 2.1.3). Thus, for assignments to the input, output, and internal variables xki a multi-valued encoding is applied. To this k that represent the respective end, each xijk is replaced by new variables yijk and zij values as follows: yijk
k zij
0 0 1 1
0 1 0 1
0 V0 1 V1
66
4 Exact Synthesis of Reversible Logic
k is assigned to 0 the Boolean domain is considered, otherwise the That is, if zij non-Boolean quantum states V0 and V1 are selected. Furthermore, since another gate library is applied, new variables to represent the respective types of a gate at depth k are required. Thus, the variables tk and ck used for Toffoli gates are replaced by qk defined as follows:
Definition 4.3 Let f : Bn → Bn be a reversible function to be synthesized as a k quantum circuit with d gates. Then, qk = (qlog . . . q1k ) with (0 ≤ k < d) is a 2 g Boolean vector defining the type of the quantum gate at depth k. The number g of gate types possible with n circuit lines is thereby defined by g = 3n(n − 1) + n. The number g of all gates types is determined as follows: Each CNOT gate, V gate, and V+ gate has exactly one target line and one control line leading to 3n(n−1) possible gate types for a circuit with n lines. Additionally, n NOT gates are possible (one at each line). Thus, in total 3n(n − 1) + n different types of quantum gates exist. Remark 4.2 If additionally double gates are considered (see Sect. 2.1.3), for a circuit with n lines in total g = 7n(n − 1) + n different types of quantum gates have to be considered. This holds, since in total four double gates exist (namely the ones shown in Fig. 2.7 on p. 16), leading to 4n(n − 1) additional types. Example 4.4 Figure 4.7 shows the variables needed to formulate the constraints for an (embedded) adder function. In comparison to the variables needed for Toffoli synthesis (see Example 4.3 or Fig. 4.6, respectively) the variables defining the type of a gate at depth k and the variables representing the circuit line values have been changed. Having these variables (enabling a multi-valued encoding considering the quantum gate library), in comparison to the Toffoli synthesis formulation the constraints are modified as follows: 0 and y d , zd leading to 1. The input/output constraints now argue over yij0 , zij ij ij n −1 n−1 2
0 yij0 = i[j ] ∧ zij = 0 ∧ yijd = f (i)[i] ∧ zdij = 0.
i=0 j =0
That is, each yij0 (yijd ) is assigned to 1 or 0 according to the j th position in the 0 (zd ) is assigned to 0, since Boolean truth table line i of f . Furthermore, each zij ij functions are synthesized. As an example, consider the left-hand and the righthand side of Fig. 4.7.
4.2 SAT-based Exact Synthesis
67
Fig. 4.7 SAT formulation for quantum circuit synthesis with n = 4 and d = 4
2. The functional constraints are modified so that the functionality of the new gate k , qk ), i.e. library is represented by a new function q(yijk , zij n −1 n−1 d−1 2
k+1 k yijk+1 zij = q(yijk , zij , qk ).
k=0 i=0 j =0
Therefore, a similar formulation as described in the last section for the Toffoli gate library is possible. 3. And finally, illegal assignments to qk are now excluded by d−1
[qk ]2 < g,
k=0
where g is given by 7n(n − 1) + n (including double gates) or 3n(n − 1) + n (without double gates), respectively. As for Toffoli circuit synthesis, in a last step this formulation has to be transformed into a CNF and passed to a SAT solver. If the solver returns satisfiable, then
68
4 Exact Synthesis of Reversible Logic
the quantum circuit can be obtained by the assignments to qk . Even for the multivalued encoding, this can be done efficiently for many practically relevant functions. However, before the performance of the encodings (for both, Toffoli circuit synthesis as well quantum circuit synthesis), is considered in detail in Sect. 4.2.4, a beneficial modification for exact synthesis of (embedded) irreversible functions is introduced in the following section.
4.2.3 Handling Irreversible Functions As described in Sect. 3.1.1, an irreversible function often must be embedded into a reversible one before synthesis can be applied. This also holds for the exact synthesis approach proposed in the last sections. As a result, constant inputs, garbage outputs, and don’t cares, respectively, may occur in the embedded functions. Thus, to determine the minimal reversible circuit for an irreversible function all possible embeddings (and therewith all possible assignments to constant inputs, garbage outputs, and don’t cares) have to be checked separately.4 But, if some slight modifications in the encoding are applied, several embeddings can be considered in parallel. It is thereby distinguished between don’t cares at the outputs (e.g. because of garbage outputs) and concrete values to be assigned to constant inputs. In the former case, only the input/output constraints are relaxed. Instead of forcing all output d ) to have concrete values, only constraints for the specified ones variables xijd (yijd zij are added. Then, the variables for don’t care conditions are left unspecified and are—if the instance is satisfiable—assigned by the SAT solver. The same is done for all constant inputs. But, since a constant input must have the same assignment in all truth table lines, an additional constraint 0 0 0 x0c = x1c = · · · = x(2 n−|c| −1)c
0 0 0 0 0 0 or y0c z0c = y1c z01c = · · · = y(2 n−|c| −1)c z(2n−|c| −1)c
is added for each constant input c, respectively. This restricts the SAT solver to 0 (y 0 z0 ) with the same value for each truth table line. assign all input variables xic ic ic Furthermore, since the constant inputs are now modeled symbolically (the value of each constant input is not fixed to 0 or 1) only 2n−|c| truth table lines have to be considered (where |c| is the number of constants). Example 4.5 Consider the incompletely embedded adder function shown in Table 4.2. The adder needs one additional variable to become reversible leading to a function with n = 4 variables, one constant input c, and two garbage outputs g1 4 In
principle, also embeddings with an arbitrary number of garbage outputs and different output permutations are possible. However, in the following only embeddings with minimal garbage and a fix output order are considered. Chapter 5 provides a further consideration of different embeddings.
4.2 SAT-based Exact Synthesis Table 4.2 Incomplete embedding of an adder
69 c
cin
x
y
cout
sum
g1
g2
–
0
0
0
0
0
–
–
–
0
0
1
0
1
–
–
–
0
1
0
0
1
–
–
–
0
1
1
1
0
–
–
–
1
0
0
0
1
–
–
–
1
0
1
1
0
–
–
–
1
1
0
1
0
–
–
–
1
1
1
1
1
–
–
and g2 , respectively.5 To handle this incompletely specified function, four modifications in the proposed SAT encoding are performed: • Constraints for only 2n−|c| = 23 = 8 (instead of 2n = 24 = 16) truth table lines are created, i.e. 0 ≤ i < 8. d (y d zd ) and x d (y d zd ) no output constraints are • For all output variables xig ig1 ig1 ig2 ig2 ig2 1 added, i.e. they are left unspecified. 0 (y 0 z0 ) no input constraints are added, i.e. they are left • For all input variables xic ic ic unspecified. 0 = x 0 = · · · = x 0 (y 0 z0 = y 0 z0 = · · · = y 0 z0 ) • An additional constraint x0c 7c 7c 7c 1c 0c 0c 1c 01c is added for each truth table line i. In summary, these modifications do not only simplify the SAT encoding (since a smaller number of truth table lines is considered), but also reduce the number of checks that have to be performed to find a minimal circuit. Normally, to ensure minimality both values for each constant input have to be considered. Thus, for a function with one constant input two checks (one with c = 0 and one with c = 1) have to be performed. Moreover, for functions with more than one constant input an exponential number of combinations has to be checked (e.g. values from {00, 01, 10, 11} for a function with two constant inputs). For all these combinations, a single instance must be encoded and separately solved by the solver. In contrast, using the proposed modifications a single instance to be checked is sufficient to synthesize a minimal result. This leads to significant speed-ups as the experiments in the next section show.
5 Note, that only a partial truth table is shown. Depending on the assignment to the constant input, n 2n−1 = 22 truth table lines with don’t care outputs are added either above or below the shown truth table lines. For more details see Sect. 3.1.1.
70
4 Exact Synthesis of Reversible Logic
4.2.4 Experimental Results The proposed approaches have been implemented in C++. To solve the resulting instances, the SAT solver MiniSAT [ES04] has been used. This section provides experimental results for both, exact synthesis of quantum circuits and exact synthesis of Toffoli circuits. More precisely, it is shown that exact synthesis can be applied with up to six variables. Improvements in the run-time can be obtained if irreversible functions containing constant inputs are considered. Furthermore, a comparison to heuristic approaches confirm the need of exact synthesis methods for both, finding smaller circuits than the currently best known realizations and evaluating the quality of heuristic methods. As benchmarks a wide range of functions from different domains has been used. This includes reversible functions as well as embedded irreversible functions. All benchmarks have been taken from RevLib [WGT+08]. The experiments have been carried out on an AMD Athlon 3500+ with 1 GB of memory. This section starts with an evaluation of the quantum circuit synthesis afterwards followed by an evaluation of exact Toffoli circuit synthesis.
4.2.4.1 Synthesis of Quantum Circuits For exact synthesis of quantum circuits the results of three evaluations are presented. First the modifications for irreversible functions as introduced in Sect. 4.2.3 are studied in detail. Next, the effect of the application of double gates on the synthesis results is observed. Finally, the presented approach is compared to a previously introduced method for quantum circuit synthesis. Handling of Irreversible Functions To synthesize quantum circuits for irreversible functions, appropriate embeddings including constant inputs, garbage outputs, and don’t care conditions are used. Then, circuits are synthesized (1) by assigning the respective constants before creating and solving the SAT instance (denoted by O RIG . SAT E NCODING) or (2) by applying the modifications proposed in Sect. 4.2.3 (denoted by I MPR . SAT E NCODING). The respective results are shown in Table 4.3. Note that if the O RIG . SAT E NCODING is applied for all possible combinations of constant input assignments a single instance is encoded and solved. Thus, for each function there are 2|c| entries, where |c| is the number of constant inputs. Each line below the function name corresponds to one assignment to the constant inputs. In contrast, if the I MPR . SAT E NCODING is applied, only a single instance has to be solved and therewith only a single result is reported. Besides that, columns labeled d show the number of gates of the resulting (minimal) circuit and columns labeled T IME list the corresponding run-time in CPU seconds (the number of variables in the function is given by n). As can be seen, the modified encoding offers a significant speed-up for all examples. The reduction of run-times is between 70% and 95%. Reductions are more substantial if at least one of the assignments has a solution with more gates than
4.2 SAT-based Exact Synthesis
71
Table 4.3 Handling of irreversible functions
O RIG . SAT E NCODING d
I MPR . SAT E NCODING T IME
d
T IME
Half-adder (n = 3)
total
4
1.47
4
0.85
5
2.65
–
–
4
4.12
4
0.85
Half-adder2 (n = 3)
total
4
1.28
4
0.76
5
3.53
–
–
4
4.81
4
0.76
297.569
Full-adder (n = 4) 6
total
6
209.95
7
1123.55
–
–
6
1421.11
6
209.95
low-high (n = 4)
total
7
10636.95
6
2180.66
6
1444.26
–
–
6
12081.21
6
2180.66
zero-one-two (n = 4)
total
7
430.30
6
132.46
6
91.23
–
–
6
204.46
–
–
6
477.84
–
–
6
1203.83
6
132.46
8
2162.51
8
5110.26
8
4391.96
–
–
8
6158.05
–
–
8
6063.92
–
–
8
18776.44
8
5110.26
decod24 (n = 4)
total
72 Table 4.4 Effect of double gates
4 Exact Synthesis of Reversible Logic Q UA .G ATES
F UNCTION n
d
D BL .G ATES
T IME
d
T IME
R EVERSIBLE FUNCTIONS 3_17
3
10
1641.49
8
280.98
miller
3
8
15.49
6
11.60
fredkin
3
7
7.04
5
3.28
peres
3
4
0.33
4
1.21
toffoli
3
5
0.71
5
2.38
peres-double
3
6
11.32
6
175.86
toffoli-double
3
7
86.75
7
1121.68
graycode6
6
5
66.50
5
608.11
q4example
4
6
9.08
5
24.83
0.40
4
0.85
E MBEDDED IRREVERSIBLE FUNCTIONS Half-adder
3
5
Half-adder2
3
4
0.19
4
0.76
Full-adder
4
7
145.07
6
209.95
rd32
4
6
37.75
6
436.11
low-high
4
7
2245.47
7
2180.60
zero-one-two
4
7
32.05
6
132.46
decod24
4
9
5660.77
8
5110.26
the optimal assignment. This can be observed for all functions in Table 4.3 except decod24. It should be noted that the constraining of constant input variables requires some computation time (i.e. run-times may be higher than those for solving the function with a fixed constant input assignment). However, this overhead is easily compensated by the fact that only one instance needs to be solved. Since the proposed modifications often lead to better results (with respect to run-time and resulting circuit size), in the following, they are also applied in the remaining experiments. Effect of Double Gates In [HSY+06], double gates (as introduced in Sect. 2.1.3) are assumed to have unit cost. However, other synthesis methods (e.g. [BBC+95, MYDM05]) consider the cost of a double gate to be two, since they are composed of two quantum gates. Hence, there are compelling reasons to consider synthesis that rely only on (single) quantum gates only. As described above, the proposed SAT-based formulation supports both, synthesis with quantum gates (denoted by Q UA . G ATES) only or additionally with double gates (denoted by D BL . G ATES). In one evaluation, circuits with double gates enabled and with double gates disabled have been considered. Disabling double gates reduces the number of possible gates at each depth from 7n(n − 1) + n to 3n(n − 1) + n (making the instance more compact). The results are summarized in Table 4.4. In the first two columns the name
4.2 SAT-based Exact Synthesis Table 4.5 Comparison to exact synthesis in [HSY+06]
73 F UNCTION
RA [HSY+06]
SAT (PIII)
T IME
T IME
I MPR
R EVERSIBLE FUNCTIONS miller
318.29
34.53
>9.2
fredkin
78.02
10.96
>7.1
peres
35.18
4.43
>7.9
toffoli
122.52
8.45
>14.5
E MBEDDED IRREVERSIBLE FUNCTIONS Half-adder
6.77
2.99
>2.3
Half-adder2
26.25
2.70
>9.7
25200.00
551.92
>45.7
Full-adder
of the function and its number of variables are given. The next columns provide the number of gates (d) and the run-time in CPU seconds (T IME) for both cases (quantum gates only and additionally with double gates). In general, it is expected that more choices of possible gates at each level will increase the time to find a correct solution. This can clearly be seen for the benchmark functions where the inclusion of double gates offers no advantage (i.e. both values for d are the same). For example, the run-time for graycode6 increases by one order of magnitude when double gates are considered—even though the results are identical with respect to the costs. On the other hand for some functions where the inclusion of double gates leads to smaller circuits (e.g. 3_17), the run-time can be reduced, since fewer instances have to be solved. Comparison with Previous Work To compare the SAT-based synthesis to the exact approach introduced in [HSY+06], a 733 MHz Pentium III with 512 MB of main memory has been used (that is significantly slower than a 850 MHz Pentium III processor, the system used in [HSY+06]). The outcome is shown in Table 4.5. RA denotes the run-time of the reachability analysis from [HSY+06] and SAT denotes the run-time of the proposed approach (using both quantum gates and double gates). IMPR gives the run-time improvement, i.e. the run-time of RA divided by the runtime of SAT. The table clearly shows that all functions from [HSY+06] considered for exact synthesis can be synthesized in significant shorter run-time—even on a slower processor. Improvements of at least a factor of 2 are achieved. In the best case an improvement of a factor of 45 is observed. Besides that, also synthesized results for the functions q4-example, Peres-double, and Toffoli-double are compared. For these functions, the authors of [HSY+06] constrained the search space, i.e. they restrict the target-line of the V and V+ gates to be a fix line. Therefore, they cannot guarantee optimal solutions. The comparison in Table 4.6 shows that the proposed approach is able to find the optimal results for these functions with a low run-time increase (the results of the heuristic approach of [HSY+06] are denoted by RAheu ). It is thereby proven that for Peres-double and
74 Table 4.6 Comparison to heurstic synthesis in [HSY+06]
4 Exact Synthesis of Reversible Logic F UNCTION
RAheu [HSY+06]
SAT (PIII)
d
T IME
T IME
peres-double
6
171.27
481.71
6
toffoli-double
7
853.78
2985.88
7
q4-example
6
34.78
78.09
5
d
Toffoli-double the minimal quantum gate circuits have been found in [HSY+06]. Additionally, in case of q4-example the SAT-based approach synthesizes an optimal quantum gate representation with costs 5 instead of the non-optimal circuit of size 6 obtained in [HSY+06]. Note, again all these benchmarks are carried out on a slower system than the one used in [HSY+06]. For absolute run-times on a faster machine see the rightmost column of Table 4.4. 4.2.4.2 Synthesis of Toffoli Circuits In a next series of experiments, the SAT-based synthesis of Toffoli circuits was considered. Since the effect of the improved handling for irreversible functions already has been evaluated above for quantum circuits, another discussion of the results for Toffoli circuits is omitted (for all cases similar results have been obtained). Instead, the respective handling is directly applied where appropriate. In the following the results of SAT-based Toffoli circuit synthesis in comparison to previously introduced exact and heuristic results are presented, respectively. Comparison to Exact Approaches In the past, [SPMH03] and [YSHP05] investigated exact synthesis of reversible functions for Toffoli circuits. However, both approaches give results only for reversible functions with up to n = 3 variables. More precisely, the overall synthesis time for all possible 23 ! = 40320 reversible functions with 3 variables is reported (at least 40 CPU seconds for [SPMH03] and 12 CPU seconds for [YSHP05]). Applying the SAT-based approach to each of these 40320 functions takes less than 0.01 CPU seconds for most of the instances and 0.65 CPU seconds in the worst case. Thus, the synthesis time for any 3-variable function is negligible. Adding up the run times for all 40320 functions would only accumulate errors of measurement. Thus, one can conclude that the overall synthesis time for a function with n = 3 is not crucial, i.e. exact synthesis for such functions can be performed efficiently. Furthermore, in contrast to [SPMH03, YSHP05] it is also possible to synthesize minimal circuits for functions with more than 3 variables as the next evaluation shows. Comparison to Heuristic Approaches Since several heuristic approaches for Toffoli circuit synthesis have been proposed so far, circuits obtained by SAT-based synthesis are not compared to the results obtained by a single previous method, but to the currently best known results. The results are shown in Table 4.7. For each function, the number of gates (d) as well as the source (S RC .) of the currently best
4.2 SAT-based Exact Synthesis
75
Table 4.7 Comparison of synthesis results E XACT
B EST KNOWN
F UNCTION n
d
QC
S RC .
T IME
d
QC
Δd
Δ QC
[MDM05]
5
13
48.28
0
0
[GAJ06]
5
9
0.60
0
0
R EVERSIBLE FUNCTIONS mod5mils
5
5
13
ham3
3
5
9
ex-1
3
4
8
[MDM05]
4
8
0.12
0
0
graycode3
3
2
2
[MDM05]
2
2
0.01
0
0
graycode4
4
3
3
[MDM05]
3
3
0.64
0
0
graycode5
5
4
4
[MDM05]
4
4
22.08
0
0
graycode6
6
5
5
[GAJ06]
5
5
583.14
0
0
3_17
3
6
14
[GAJ06]
6
14
0.43
0
0
mod5d1
5
8
24
[WGT+08]
7
11
2094.13
1
13
mod5d2
5
8
16
[MDM05]
8
20
1616.07
0
−4
E MBEDDED IRREVERSIBLE FUNCTIONS rd32
4
4
12
[GAJ06]
4
12
3.03
0
0
decod24
4
11
31
[GAJ06]
6
18
6.33
5
13
4gt4
5
17
89
[MDM05]
5
54
412.03
12
35
4gt5
5
13
29
[MDM05]
4
28
48.75
9
1
4gt10
5
13
53
[MDM05]
5
37
245.60
8
16
4gt11
5
12
16
[MDM05]
3
7
7.32
9
9
4gt12
5
14
58
[MDM05]
5
41
440.30
9
17
4gt13
5
14
34
[MDM05]
3
15
7.23
11
19 0
4mod5
5
5
9
[WGT+08]
5
9
125.74
0
4mod7
5
6
38
[MDM05]
6
38
653.82
0
0
one-two-three
5
11
71
[MDM05]
8
24
2186.71
3
47
alu
4
18
114
[GAJ06]
6
22
2001.32
12
92
known realization is given in column B EST KNOWN.6 In contrast, column E XACT shows the number of gates obtained by the SAT-based synthesis. The quantum costs for the respective circuits are denoted by QC. The difference with respect to the number of gates and quantum costs is given in the last two columns, respectively. Since the results from previous work have been obtained by different approaches on different machines, no run-times for these are reported. The run-time of the SAT-based synthesis is given in column T IME. Using the exact synthesis, it can be proven that for many functions minimal Toffoli circuits (with respect to gate count) already have been found (all rows with Δd equal to 0). That shows, today’s synthesis approaches are achieving very good 6 For
some functions no results have been reported before. In this case, the approach of [MDM05] has been applied to generate a heuristic result.
76
4 Exact Synthesis of Reversible Logic
results for these benchmarks. However, exact synthesis additionally enables the realization of significantly smaller circuits. For example, the circuit for 4gt5 can be reduced by more than two third. In absolute numbers, up to 12 gates can be saved for some functions. Moreover, the proposed approach also improves the quantum cost for many functions (only in one case (for mod5d2) the quantum cost increase.7 In the best case the quantum costs are reduced by 92. It can be concluded that using SAT-based synthesis as proposed, exact results can be produced for functions with up to six variables. The comparison to heuristic approaches confirm the need of exact methods for both, finding smaller circuits than the currently best known realizations and evaluating the quality of heuristic methods. However, synthesizing exact results still requires high computing times. Thus, in the next section improvements are proposed that accelerate the synthesis process.
4.3 Improved Exact Synthesis In the last section, SAT-based synthesis for quantum gate and Toffoli gate circuits has been demonstrated as a promising alternative to achieve minimal results for functions with up to six variables. But, the proposed approach still works on a problem description in CNF. In this section, two improvements are described exploiting higher levels of abstraction and leading to an increase of both, efficiency and quality of the obtained results. Therefore, from now on the focus is on circuits composed of reversible gates only. But, the described techniques can also be applied to synthesis of quantum circuits. In the first part of this section, the SAT encoding presented in the last section is used as basis but lifted to a higher level of abstraction. More precisely, the synthesis problem is encoded in bit-vector logic which can be solved by Satisfiability Modulo Theories (SMT) solvers instead of Boolean SAT solvers. Experiments show that this leads to significant speed-ups. However, after a detailed analysis of the fundamental limits of these solving paradigms, another approach is proposed that utilizes problem-specific knowledge. Consequently, the general solver framework SWORD (see Sect. 2.3.2.3) is applied for which dedicated modules have been specified. Besides a high-level and very compact problem representation, these modules allow more efficient decision and propagation strategies. But even with higher levels of abstractions, the size of the synthesis encoding is still exponential. That is, constraints are built for each truth table line of the function f to be synthesized. As an alternative, in the second part of this section an approach for reversible logic synthesis which leads to a polynomial size encoding is proposed. This encoding takes advantage of Quantified Boolean Formula (QBF) 7 This is because circuits are optimally synthesized with respect to number of gates not quantum cost. In some (few) cases circuits with larger number of gates but with lower quantum cost are possible. For results with respect to quantum gates (and therewith with respect to quantum cost) see the discussion above.
4.3 Improved Exact Synthesis
77
satisfiability—a generalization of Boolean satisfiability (see Sect. 2.3.1). More precisely, the exact synthesis problem of a reversible function f is formulated as a QBF problem by encoding the cascade structure of a reversible circuit as a functional composition of universal gates and enforcing to meet the specification of f by quantification. In this sense, complexity is moved from the problem formulation to the solving engine. Then, the quantified Boolean formula is solved by applying QBF solvers and Binary Decision Diagrams (BDDs). This leads to three major improvements: (1) the circuits are synthesized faster, (2) all minimal circuits are found in a single step which allows to choose the best one with respect to the quantum cost, and (3) different reversible gate libraries are easily supported by a simple extension of the problem formulation. In the remainder of this section, both approaches are described in detail in Sect. 4.3.1 (for SMT and SWORD) and Sect. 4.3.2 (for OBF), respectively. Afterwards, experimental results for both are presented in Sect. 4.3.3.
4.3.1 Exploiting Higher Levels of Abstractions Recalling the proposed SAT-based encoding, for a function f : Bn → Bn to be synthesized as a circuit consisting of d gates, the variables k • tk = (tlog
2 n
. . . t1k ) (to define the target line of a gate at depth k),
k ck k • ck = (cn−1 n−2 . . . c1 ) (to define the control lines of a gate at depth k), and k k k • xi = (xi(n−1) . . . xi0 ) (to represent the input, output, and internal variables)
as well as the constraints n • 2i=0−1 [x0i ]2 = i ∧ [xdi ]2 = f (i) (input/output), n k+1 = t (xki , tk , ck ) (functional), and • 2i=0−1 d−1 k=0 xi k • d−1 k=0 [t ]2 < n (exclusion) have been introduced. As can be seen, a large part of the problem formulation consists of bit-vector variables and bit-vector constraints, respectively. However, most of this high level of abstraction is lost, when the formulation is encoded as a pure Boolean formula and afterwards solved by a Boolean SAT solver. Furthermore, this transformation requires a high amount of auxiliary variables leading to an additional overhead. Thus, it is worth to consider alternative encodings. The emerging area of SAT Modulo Theories (SMT) (see Sect. 2.3.2) provides new solving engines that directly support bit-vector logic and thus allow an encoding that avoids the conversion to the Boolean level. As a result, all bit-vector variables and most of the bit-vector operations are preserved; hardly any auxiliary variables are needed. Furthermore, the formulation at this higher level of abstraction allows stronger implications. As the experiments in Sect. 4.3.3 show, already this simple “replacement” allows significant improvements in the resulting synthesis times.
78
4 Exact Synthesis of Reversible Logic
However, further accelerations can be achieved if more dedicated solving engines are exploited. One adjusted solving technique, based on the framework SWORD [WFG+07], is described in this section. The general limits of the SAT- and SMT-based approaches are thereby discussed first. Then, the dedicated implication procedures and decision heuristics are introduced.
4.3.1.1 Limits of Common SAT and SMT Solvers The input of a SAT solver is a Boolean function in terms of clauses. The input of an SMT solver is a description in bit-vector logic. Both solvers are optimized for their particular problem representation. For example, common SAT solvers utilize the two literal watching scheme to carry out implications, which exploits the special structure of clauses [MMZ+01]. SMT solvers on the other hand, use e.g. canonizing [BDL98] and term-rewriting [BB09] to efficiently handle bit-vector constraints. Furthermore, highly optimized heuristics have been developed to decide the assignment of variables if no more implications are possible. Strategies employed are based on statistical information, for example occurrences or activities of variables [Mar99]. All these techniques work very well if CNF formulas or bit-vector logic are considered in general. But, the respective solvers are not able to take specific properties of the problem into account. For example, promising problem-specific strategies for the exact Toffoli circuits synthesis would be: • The type of the Toffoli gates (represented by tk and ck ) near to the inputs should be defined first, because the corresponding input variables are already assigned by the truth table. This allows for early implications and helps to determine the types of the remaining gates or to detect conflicts faster. Thus, tk and ck with small k should be preferred in the decision procedure. Similarly, this observation also holds for modules near to the outputs. • If the assignment to an input line of a Toffoli gate is not equal to the assignment to the corresponding output line of the same gate, this line has to be the target line. This observation allows to imply the assignment to variables in tk . • If the target line of a Toffoli gate is known, the values of all remaining lines can be implied if there is an assignment at the corresponding input or output. These specific strategies cannot be provided by a standard SAT or SMT solver. Moreover, extensions of standard solvers in this direction (e.g. by modifications of the heuristics) are not possible in general, because most of the problem-specific information is lost when encoding the instance. SAT and SMT solvers just have a clause database or constraint database, respectively. Thus, strategies like the ones described above can only be exploited with a solver that is based on a problemspecific representation.8 8 In principle, this problem can be prevented by introducing additional constraints to the problem instance. But then, the encoding becomes inefficient due to a very large number of constraints.
4.3 Improved Exact Synthesis
79
4.3.1.2 Dedicated Solve Techniques for Toffoli Circuit Synthesis To overcome the limitations discussed above, the solver framework SWORD is applied. While SAT solvers provide strategies optimized for clauses and SMT solvers for bit-vector constraints, respectively, SWORD makes problem-specific information available by using so called modules. These modules enable the implementation of dedicated heuristic as well as implication strategies, while still utilizing sophisticated SAT techniques such as conflict analysis or learning. In the following, the application of SWORD to Toffoli circuit synthesis is described. Section 2.3.2.3 gives a brief overview of the underlying solving techniques (starting on p. 24). For Toffoli circuit synthesis, dedicated modules have been developed that incorporate the problem-specific strategies described above. More precisely, a concrete Toffoli synthesis instance for the reversible function f to be synthesized with d gates includes d modules in a cascade structure—one module for each depth k. Each module has access to its relating variables tk , ck , xki , and xk+1 . The functionali ity of a Toffoli gate is defined by methods of the module, i.e. a concrete Toffoli gate function is selected by assigning tk and ck . Then, each module realizes the decision and implication strategies as described in the following. Decision Strategies The decision heuristic chooses a variable to be assigned if no further implication is possible. Therefore, decisions that cause many implications are preferred. Both, the global decision strategy (deciding which module should make the next decision) and the local decision strategy (deciding which variable of the chosen module should be assigned next) are motivated by the following two observations: • A module can imply many other assignments if the target line t k of the represented gate is known (i.e. if tk is completely assigned). In this case, the input xki and the output xk+1 for all lines, except the target line, have to be equal. i • The assignment of nearly all gate inputs xki are either given by the truth table (if k = 0) or they can be implied if the types of the previous gates are defined. These observations lead to the following decision heuristics: • Global decision heuristic Modules whose target lines are still undefined are selected for a decision. If all target lines of each module are defined, the module that still has other unassigned variables (from the vector ck for example) is selected. During this process the modules are considered in ascending order starting with depth k = 0. • Local decision heuristic Variables representing the target line of the respective Toffoli gate are decided first. If all target line variables are assigned, variables corresponding to the control lines are decided. Since the overall decision strategy (global and local) ensures that the gates become completely defined from the first gate to the last gate, there is no need for deciding the variables representing the inputs or outputs. These variables are implied after the corresponding types of the previous gates are decided.
80 Fig. 4.8 Propagate routine for module at depth k
4 Exact Synthesis of Reversible Logic
(1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (11) (12) (13) (14) (15) (16) (17) (18) (19)
for each (truth table line i) for each (circuit line j ) k = x k+1 // input = output if (xij ij imply(tk ); // use value of j for each (circuit line j ) if (j == [tk ]2 ) continue; k or x k+1 ); imply(xij ij flipTargetLine = true; for each (clk ∈ ck ) if (clk == 1 ∧ x k k
it +l mod n)
== 0)
flipTargetLine = false; break; if (!flipTargetLine) imply(x k+1 ); // use value of x k k it k it else if (ck completely defined) k imply(x k+1 k ); // use value of x k ⊕ 1 it
it
Propagation Strategies The propagation procedures of a module consider the connected variables for the implication of the values. The pseudo-code of the propagation routine is shown in Fig. 4.8. The propagation routine consists of three parts: 1. Propagate the position of the target line (lines 2–4) If the assignment to an input is not equal to the assignment to the corresponding output of the same circuit line, then this line has to be the target line of the gate. In this case, the position j of this target line is assigned to tk . 2. Propagate non-target lines (lines 6–8) If the target line is known, all outputs are implied whose corresponding inputs are assigned (except the ones at the target line). This also holds vice versa. Thus, the output (input) is assigned to the value of the corresponding input (output). 3. Propagate the target line (lines 10–19) In the last step the output of the target line is assigned. To do so, the assignment to the control lines ck and to the corresponding input variables are considered. If a circuit line is a control line and the input of this line is assigned to 0 (line 12), then the assignment to the output of the target line has to be equal to its corresponding input assignment (line 16). Otherwise, if additionally ck is completely defined (i.e. no other control line with input value 0 can occur), the assignment to the output of the target line has to be equal to the inverted assignment to the input of this line (line 19). The presented decision and propagation strategies incorporated in the modules replace the respective functional and exclusion constraints. Furthermore, due to the problem-specific checks and heuristics, the overall formulation is lifted to a higher level of abstraction leading to faster run-times as shown in the evaluation
4.3 Improved Exact Synthesis
81
in Sect. 4.3.3. Nevertheless, the applied formulation still has to consider all truth table lines of f , i.e. the formulation is still of exponential size. How to overcome this drawback is described in the next section.
4.3.2 Quantified Exact Synthesis So far, for the respective checks “Is there a circuit with exactly d gates that realizes the given reversible function f ?”, several encodings on different levels of abstractions, i.e. on Boolean level, bit-vector level, or problem-specific level have been introduced. In all variations the problem is encoded for each truth table line separately. That is, the respective constraints representing the circuit to be synthesized are not built only for one truth table line, but they are duplicated for the remaining 2n − 1 truth table lines. Thus, the instances grow exponentially with respect to the number n of variables. In this section, an alternative problem formulation based on Quantified Boolean Formulas (QBF) (see Sect. 2.3.2) is introduced. QBF allows to encode the synthesis problem in polynomial size, i.e. the circuit to be synthesized is encoded only once and the specification of the considered function f is enforced by quantification. In doing so, complexity is moved from the problem description to the solving engine. In the following, the concrete method is described using a new formulation based on a universal gate type definition. This does not only enable to synthesize Toffoli circuits, but also reversible circuits consisting of Fredkin and Peres gates, respectively. Finally, it is shown how the resulting formulation can be solved using QBF solvers and Binary Decision Diagrams (BDDs). 4.3.2.1 Quantified Problem Formulation For the synthesis of a function f with n inputs/outputs into a reversible circuit, a set GT = {g0 , . . . , gq−1 } of q ∈ N different gate types is considered. The set GT is used to distinguish between all possible gate types in n variables. According to the chosen gate library (i.e. Toffoli gates, Fredkin gates, and/or Peres gates), the cardinality of GT varies. More precisely, let f : Bn → Bn be a reversible function to be synthesized. Then, there are • n · 2n−1 different multiple control Toffoli gate types, • n · (n − 1) · 2n−2 different multiple control Fredkin gate types, and • n · (n − 1) · (n − 2) different Peres gate types. If the gate library used for synthesis consists of more than one gate type, then the numbers above have to be added. For example, in the case of a gate library containing multiple control Toffoli gates and multiple control Fredkin gates for the synthesis of a 3 variable function, GT contains 3 · 23−1 + 3 · (3 − 1) · 23−2 = 12 + 12 = 24 different gates in total. Before the synthesis problem is formulated as a QBF instance, the universal gate is defined that covers the functionality of all gates given in the set GT.
82
4 Exact Synthesis of Reversible Logic
Definition 4.4 Let f : Bn → Bn be a reversible function to be synthesized. Then, a universal gate represents the function U GT (X, Y ) : Bn × Blog q → Bn with gt (X), if t = [y1 . . . ylog q ]2 < q, GT U (X, Y ) = X, otherwise where • X = {x1 , . . . , xn } is the set of the inputs of the gate and • Y = {y1 , . . . , ylog q } is the set of variables representing a binary encoding of a natural number t, which defines the type gt of the gate (in the following called gate select inputs). According to the assignments to the gate select inputs Y , a universal gate U GT acts either as a gate from the given set GT or as the identity gate. Remark 4.3 The variables Y = {y1 , . . . , ylog q } are comparable to the variables tk and ck used in the previous sections to define the type of a Toffoli gate. However, since now Fredkin and Peres gates are additionally considered, tk and ck cannot be applied any longer and thus are replaced by Y . Furthermore, the identity gate has been added to the definition of a universal gate to handle the case where the set GT does not exactly contain a power of two gate types. In this case, GT is extended by identity gates to fill the gap. In doing so, exclusion constraints are not needed any longer. Having a universal gate as a basis, a cascade of universal gates is defined as follows: Definition 4.5 Let f be a reversible function to be synthesized with at most d gates from the set GT. Then, a function F d is built representing the cascade structure of d universal gates U GT (X1 , Y1 ), . . . , U GT (Xd , Yd ). The output of the i-th universal gate (0 < i ≤ d) is equal to the input of the next gate, i.e. U GT (Xi , Yi ) = Xi+1 . Figure 4.9 shows the resulting cascade structure of the function F d for d universal gates. Using this structure, any reversible circuit containing d gates can be obtained by assigning the respective values to each of the gate select input variables yij ∈ Yi (0 < j ≤ log q). In other words, if a circuit realization with at most d gates for the reversible function f exists, there has to be at least one assignment to all variables yij ∈ Yi such that F d is equal to f . More formally, if f is synthesizable with at most d gates the quantified Boolean formula ∃y11 . . . ∃ydlog q ∀x1 . . . ∀xn (F d = f ) holds. This represents the new encoding of the synthesis problem which can be solved either by a QBF solver or by BDDs as described in the following.
4.3 Improved Exact Synthesis
83
Fig. 4.9 Problem formulation
4.3.2.2 Implementations Based on the proposed QBF formulation for the synthesis problem, two approaches can be applied to solve the formula: First, the problem is encoded as an instance of quantified Boolean satisfiability, which is given to a QBF solver. Second, the function F d = f is constructed as a BDD and thereafter the quantification is carried out on the BDD. A solution exists if the final BDD is not the constant 0-function. Moreover, all solutions can be extracted by traversing all paths to the 1-terminal. For both approaches, the incremental nature of F d is exploited during the construction of the formula. That is, first the formula F 0 = (x1 , . . . , xn ) is built for depth d = 0. Then, for each iteration, the function F d is incrementally built by applying F d = U GT (U GT (. . . (U GT (F 0 , Y1 ), Y2 ) . . . , Yd−1 ), Yd ). Finally, the equation to f is constrained. The next two paragraphs describe the respective steps for both approaches in more detail. Using QBF Solvers To use a common QBF solver, the formula F d = f is transformed into CNF, i.e. a representation that consists of Boolean variables and clauses. Then, the resulting set of clauses represents a cascade of d universal gates which has to meet the specification of f . The complete QBF instance is formed by adding the respective existential and universal quantifiers followed by an existential quantifier for the auxiliary variables added during the transformation into CNF (denoted as A in the following). Overall, this leads to the following quantification: ∃y11 . . . ∃ydlog q ∀x1 . . . ∀xn ∃A. Together with the CNF, afterwards this is passed to a QBF solver. In the case that the instance is satisfiable, a circuit realization of the function can be obtained from the assignments to the variables yij ∈ Yi . Otherwise it has been proven that no circuit realizing f with d gates exists. Using BDDs As shown later in the experiments, the performance of the QBF solver approach is poor. Therefore, BDDs are used as an alternative. That is, instead of building a quantified CNF and solving this instance with a QBF solver, the synthesis is carried out on a BDD representation.
84
4 Exact Synthesis of Reversible Logic
To this end, the BDD for the formula F d = f is build. This can be done efficiently using a state-of-the-art BDD package (e.g. CUDD [Som01]). The fixed variable order X, Y has thereby been applied. The alternative order Y, X leads to a blow-up of the BDD representation, since in this case, the BDD for F d would already represent all possible functions in n variables which are synthesizable with at most d gates. During the construction, isomorphic functions that result from the n output functions for F d are shared. After the computation of the equality, the resulting BDD is a single output function. For this BDD, the universal quantification of all xi variables is carried out. This is a standard operation available in a BDD package. The idea is to compute the product of the positive co-factor and the negative co-factor for a universally quantified variable, i.e. ∀x h(. . . , x, . . .) = h(. . . , 0, . . .) · h(. . . , 1, . . .). If the final BDD consists of the 0-terminal, then no reversible circuit with the given depth d exists for the function f . Otherwise, there is at least one path to the 1-terminal. Each of those paths represents an assignment to all variables yij ∈ Yi and thus can be converted into a concrete circuit realization. Since the BDD represents not only one but all 1-paths, in fact all realizations with the given depth are found in one single step. All solutions are of interest, since one can choose the best result with respect to quantum cost which is discussed later in the experiments.
4.3.3 Experimental Results The proposed improvements for exact synthesis have been implemented in C++. According to the respective encoding, the SMT solver MathSAT [BBC+05], the solver framework SWORD [WFG+07] (see also Sect. 2.3.2.3), the QBF solver sKizzo [Ben05], and the BDD package CUDD [Som01] have been used as solving engine, respectively. In this section, the described encodings are compared to each other as well as to the SAT-based encoding with MiniSAT [ES04] as solver. It is shown that higher levels of abstractions significantly improve the run-time when performing exact synthesis. Moreover, using the quantified formulation, further speed-ups can be documented and additionally the quality of the results can be strengthened. As benchmarks, again functions from RevLib [WGT+08] have been applied. All experiments have been carried out on an AMD Athlon 3500+ with 1 GB of memory. The timeout was set to 3000 CPU seconds.
4.3.3.1 Exploiting Higher Levels of Abstractions The original SAT encoding for exact synthesis (denoted by SAT) has been lifted to two higher levels of abstractions. First, instead of a Boolean formulation in CNF, the problem has been encoded in bit-vector logic that can be handled by SMT solvers (denoted by SMT). Second, problem-specific strategies developed within the solver framework SWORD provide an alternative (denoted by SWORD).
4.3 Improved Exact Synthesis
85
Results obtained by both approaches are summarized in Table 4.8. The first column provides the name of the function. Column n denotes the number of variables for each function, while in Column d the minimal number of Toffoli gates necessary to synthesize the function is given. The following columns provide the run-time of the respective synthesis approaches in CPU seconds (denoted by T IME). Furthermore, the improvements of the SMT approach and the SWORD approach are given in the last two columns, i.e. the run-time of MiniSAT divided by the run-time of MathSAT/SWORD (denoted by I MPR SAT ) and the run-time of MathSAT divided by the run-time of the SWORD approach (denoted by I MPR SMT ), respectively. The results clearly show that the chosen encoding is crucial for the resulting runtimes. For most of the functions a corresponding Toffoli circuit can be synthesized faster by the SMT approach than by using the SAT encoding. Only in some cases the SAT-based approach is slightly better. However, this only holds for functions that can be synthesized in less than one second, e.g. peres or fredkin. Overall, improvements of up to three orders of magnitude are achieved. Moreover, it is evident that the problem-specific approach outperforms the other methods. In many cases, the run-times are further reduced by a factor of approx. 30—in the best case by a factor of over 170. Furthermore, using the problemspecific approach, Toffoli circuits for the functions alu-v2 and alu-v3 are synthesized within the given timeout. In comparison to the SAT synthesis approach, speed-ups of up to four orders of magnitude are reported.
4.3.3.2 Quantified Exact Synthesis To evaluate the quantified exact synthesis encodings, three series of experiments have been carried out. First, the performance of the QBF solver and the BDD approach, respectively, is compared to each other as well as to previous encodings. Second, the fact that (using the BDD approach) all circuits for a given depth are available in parallel is evaluated. And finally, synthesis results for different gate libraries are considered. Run-time Comparison The run-times of both, the proposed QBF solver encoding (denoted by QBF S OLVER) as well as the BDD formulation (denoted by BDD S), is compared to the SAT-based and the SWORD-based approach (denoted by SAT and SWORD, respectively). A similar set of functions as in the previous sections was applied. Only some trivial functions (e.g. peres, fredkin) have been omitted. In contrast, two further functions, i.e. hwb4 and 4_49 (also taken from [WGT+08]), are additionally considered. The results are given in Table 4.9. The first columns show the name of the function as well as the number of lines (n) and the minimal number of Toffoli gates (d) of the resulting circuit, respectively. In the remaining columns, the run-times in CPU seconds (denoted by T IME) and the improvements of the new approaches with respect to the SAT solver (denoted by I MPR SAT ) and with respect to SWORD (denoted by I MPR SW. ) are given, respectively. The improvement is thereby obtained by the run-time of the SAT/SWORD approach divided by the run-time of the QBF solver/the BDD approach.
86
4 Exact Synthesis of Reversible Logic
Table 4.8 Experimental results if higher levels of abstractions are exploited F UNCTION n
d
SAT
SMT
T IME
T IME
SWORD I MPRSAT
T IME
I MPRSAT
I MPRSMT
R EVERSIBLE FUNCTIONS peres
3
2
0.01
0.03
0.33
<0.01
fredkin
3
3
0.03
0.12
0.25
peres-double
3
4
2.35
0.36
6.53
miller
3
5
0.23
0.22
mod5mils
5
5
48.28
3.81
>1.00
>33.00
<0.01
>3.00
>24.00
0.01
235.00
36.00
1.05
<0.01
>23.00
>22.00
12.67
0.08
603.50
47.63
ham3
3
5
0.60
0.29
2.07
0.01
60.00
29.00
ex-1
3
4
0.12
0.20
0.60
<0.01
>20.00
>12.00
graycode3
3
2
0.01
0.06
0.17
<0.01
>1.00
>6.00
graycode4
4
3
0.64
0.24
2.27
<0.01
>64.00
>24.00
graycode5
5
4
22.08
1.00
22.08
<0.01
graycode6
6
5
583.14
3.25
179.43
0.12
4859.50
27.08
3_17
3
6
0.43
0.72
0.59
0.03
14.33
24.00
mod5d1
5
7
2094.13
135.36
15.47
11.21
186.80
12.07
>2208.00 >100
mod5d2
5
8
1616.07
56.72
28.49
9.06
178.37
6.26
mini_alu
4
5
27.60
3.85
7.17
0.03
920.00
123.33
E MBEDDED IRREVERSIBLE FUNCTIONS rd32-v0
4
4
2.97
0.54
5.50
<0.01
>297.00
>54.00
rd32-v1
4
5
13.51
1.84
7.34
0.04
337.75
46.00
decod24-v0
4
6
6.54
1.33
4.92
0.02
327.00
66.50
decod24-v1
4
6
6.22
1.44
4.32
0.09
69.11
16.00
decod24-v2
4
6
7.25
1.35
5.37
0.02
362.50
67.50
decod24-v3
4
7
28.88
3.31
8.73
0.18
160.44
18.39
4gt4-v0
5
6
>3000.00
697.37
>4.30
21.19
>141.58
32.91
4gt4-v1
5
5
395.36
33.31
11.87
0.62
637.67
53.73
4gt5-v0
5
5
321.27
36.92
8.70
0.41
783.59
90.05
4gt5-v1
5
4
51.51
10.35
4.98
0.06
858.50
172.50
4gt10-v0
5
5
229.01
47.97
4.77
5.31
43.13
9.03
4gt10-v1
5
6
2417.04
234.85
10.29
14.69
164.54
15.99
4gt11-v0
5
3
8.54
1.32
6.47
0.01
854.00
132.00
4gt11-v1
5
4
33.39
3.30
10.12
0.24
139.13
13.75
4gt12-v0
5
5
441.79
25.86
17.08
2.39
184.85
10.80
4gt12-v1
5
5
470.96
41.17
11.44
9.53
49.42
4.32
4.3 Improved Exact Synthesis
87
Table 4.8 (Continued) F UNCTION
SAT
SMT
T IME
T IME
SWORD I MPRSAT
T IME
I MPRSAT
I MPRSMT
n
d
4gt13-v0
5
3
6.75
0.74
9.12
0.01
675.00
74.00
4gt13-v1
5
4
32.99
4.35
7.58
0.21
157.10
20.71
4mod5-v0
5
5
122.54
12.70
9.65
0.69
177.59
18.40
4mod5-v1
5
5
413.21
43.86
9.42
0.48
860.85
91.38
4mod7-v0
5
6
665.66
34.68
19.19
2.99
222.63
11.60
4mod7-v1
5
7
2055.80
100.07
20.54
133.97
15.34
0.75
one-two-three-v0
5
8
2292.26
443.21
5.17
55.66
41.18
7.96
one-two-three-v1
5
8
2094.26
481.53
4.35
71.72
29.20
6.71
one-two-three-v2
5
8
>3000.00
609.96
>4.91
78.05
>38.44
7.81
one-two-three-v3
5
8
>3000.00
250.36 >11.98
136.00
>22.06
1.80
alu-v0
5
6
1998.83
223.48
8.94
8.76
228.18
25.51
alu-v1
5
7
>3000.00
1692.29
>2.95
369.14
>13.54
4.58
alu-v2
5
7
>3000.00 >3000.00
–
840.25
>3.57
>3.57
alu-v3
5
7
>3000.00 >3000.00
–
764.04
>3.93
>3.93
From the results, it is easy to see that utilizing QBF leads to significant improvements for both, the QBF solver and the BDD approach, in comparison to the common SAT solving techniques. Only if additional knowledge is utilized, as done by SWORD, the QBF solver method is outperformed. However, the BDD approach for QBF leads to the smallest overall synthesis time for non-trivial functions. That is, for some functions the run-time, indeed, is higher than for SWORD, but this only holds for functions with an overall synthesis time of less than one second (e.g. graycode6 and decod24-v0). For all other functions, better run-times are documented. In the best case (hwb4), an improvement of more than a factor of 100 is achieved. Quantum Costs of Resulting Circuits After the efficiency of the BDD approach has been shown with respect to run-time, further experiments demonstrate the quality of the obtained results. As described in the preliminaries, quantum costs provide a good measurement of the complexity of the resulting circuits. The quantum costs thereby depend on the used Toffoli gates. Thus, it may be an advantage to determine not only one, but more Toffoli circuits for a given function. Then, by checking the resulting quantum costs for each of the realizations obtained the cheapest one with respect to quantum costs can be selected. Previous approaches for minimal Toffoli circuit synthesis determine only one circuit in each run. In contrast, using BDDs as described in Sect. 4.3.2 leads to
88
4 Exact Synthesis of Reversible Logic
Table 4.9 Comparison of quantifed encodings QBF- BASED
SAT- BASED
F UNCTION
SAT
SWORD QBF S OLVER
n d T IME R EVERSIBLE
T IME
T IME
BDD S
I MPRSAT I MPRSW. T IME
I MPRSAT I MPRSW.
FUNCTIONS
mod5mils
5
5
48.28
0.08
32.22
1.50
<0.01
0.15
321.87
0.53
graycode6
6
5
583.14
0.12
145.02
4.02
<0.01
0.46 1267.69
0.33
3_17
3
6
0.43
0.03
0.19
2.26
0.16
0.01
43.00
3.00
mod5d1
5
7
2094.13
11.21
405.96
5.16
0.03
1.68 1246.50
6.67
mod5d2
5
8
1616.17
9.06
337.49
4.79
0.03
3.84
2.36
hwb4
4 11 >3000.00 >3000.00 >3000.00
–
–
4_49
4 12 >3000.00 >3000.00 >3000.00
–
–
420.88
20.38 >147.20 >147.20 837.92
>3.58
>3.58
E MBEDDED
IRREVERSIBLE FUNCTIONS
rd32-v0
4
4
2.97
<0.01
0.22 13.50
<0.05
0.01
297.00
<1.00
rd32-v1
4
5
13.51
0.04
0.35 38.60
0.11
0.03
450.33
1.33
4mod5-v0
5
5
122.54
0.69
40.01
3.06
0.02
0.20
612.70
3.45
4mod5-v1
5
5
413.21
0.48
44.63
9.25
0.01
0.16 2582.56
3.00
decod24-v0 4
6
6.54
0.02
0.97
6.74
0.02
0.04
0.50
decod24-v1 4
6
6.22
0.09
1.28
4.86
0.07
0.04
155.50
2.25
decod24-v2 4
6
7.25
0.02
1.03
7.04
0.02
0.03
241.66
0.66
decod24-v3 4
7
28.88
0.18
2.00 14.44
0.09
0.05
577.60
3.60
1998.83
8.76
181.99 10.98
0.05
2.73
alu-v0
5
6
alu-v1
5
7 >3000.00
163.50
732.17
3.21
369.14 >3000.00
–
–
30.42 >98.62
12.13
alu-v2
5
7 >3000.00
840.25 >3000.00
–
–
34.72 >86.41
24.20
alu-v3
5
7 >3000.00
764.04 >3000.00
–
–
45.69 >65.66
16.72
all possible circuits in parallel. The differences in the resulting quantum costs are documented in Table 4.10. Column #S OL denotes the number of solutions found by the BDD approach, while QC denotes the minimal as well as the maximal quantum costs for the determined realizations. Considering the quantum costs of the obtained Toffoli circuits lead to further significant improvements. For example, circuits representing function 4_49 have quantum cost of 32 in the best case, while in the worst case quantum cost of more than 70 are required. Thus, in contrast to previous algorithms, the BDD-based synthesis is not only faster but also another quality criterion—the resulting quantum costs—is applicable. Synthesis with Extended Libraries Finally, the application of further gate types to the BDD-based synthesis is shown. This is done by extending the universal gate formula with further gates, i.e. Fredkin and Peres gates.
4.3 Improved Exact Synthesis Table 4.10 Quantum costs of resulting circuits
89 F UNCTION
#S OL
d
QC
R EVERSIBLE FUNCTIONS mod5mils
5
12
13–13
graycode6
5
1
5–5
3_17
6
7
14–14
mod5d1
7
1208
11–15
mod5d2
8
135
12–20
hwb4
11
264
23–39
4_49
12
374
32–72
E MBEDDED IRREVERSIBLE FUNCTIONS rd32-v0
4
4
12–12
rd32-v1
5
20
13–13
4mod5-v0
5
1176
9–21
4mod5-v1
5
592
9–25
decod24-v0
6
75
10–34
decod24-v1
6
3
14–22
decod24-v2
6
23
14–26
decod24-v3
7
1950
11–43
alu-v0
6
824
14–38
alu-v1
7
850
15–27
alu-v2
7
16296
15–55
alu-v3
7
132
15–39
The results are shown in Table 4.11. The respective depth (d), the run-time of the synthesis (T IME), the number of solutions (#S OL), and the quantum costs (QC) are listed. MCT+MCF denotes the results for a set of gates including multiple control Toffoli and multiple control Fredkin gates, MCT+P denotes the results for the set of gates including multiple control Toffoli and Peres gates, and MCT+MCF+P denotes the results for the set of all three gate types. As expected, extending the gate library leads to smaller realizations as for example the results for hwb4 show. While the minimal MCT-circuit for this function consists of eleven gates, it can be reduced by three more gates if additionally Peres gates are used. Furthermore, improvements with respect to the number of gates can be achieved for alu, 3_17, mod5d2, 4_49, rd32 and decod24, respectively. However, with an increasing number of gates to be considered, the run-times increase as well. This can be seen e.g. for function 4_49 or 4mod5. Only for the functions where the extension of the gate library leads to smaller circuits, the runtimes sometimes decrease (e.g. for function alu with the MCT+MCF library), since fewer iterations of the main flow have to be performed (see Sect. 4.1).
n
d
T IME
MCT+MCF
4
4_49
5
10
9
8
7
5
5
0.52
1193.90
37.36
63.96
15.08
0.02
2.77
4
4
5
5
4
4
4
4
5
5
5
5
rd32-v0
rd32-v1
4mod5-v0
4mod5-v1
decod24-v0
decod24-v1
decod24-v2
decod24-v3
alu-v0
alu-v1
alu-v2
alu-v3
5
5
5
4
6
6
5
5
5
5
5
4
10.00
8.66
6.73
0.43
0.07
0.09
0.04
0.05
3.52
4.09
0.10
0.02
I NCOMPLETELY S PECIFIED F UNCTIONS
5
mod5d1
4
5
3_17
hwb4
3
graycode6
mod5d2
5
6
mod5mils
C OMPLETELY S PECIFIED F UNCTIONS
F UNCTION
126
224
114
22
292
1435
12
13
2792
3672
780
4
32
774720
135
1352
2
1
12
#S OL
Table 4.11 Synthesis results using other gate libraries
17–33
17–39
17–33
16–30
12–32
12–38
15–23
11–25
9–29
9–23
11–41
12–12
54–58
31–51
12–20
11–19
15–15
5–5
13–13
QC
–
6
6
6
5
5
5
4
4
5
3
2
–
8
6
7
5
5
5
>3000.00
189.73
202.87
181.43
0.06
0.06
0.05
0.03
0.25
2.96
0.02
0.01
>3000.00
10.03
4.61
63.62
0.02
4.22
0.90
T IME
MCT+P d
–
280
638
29900
300
180
268
5
768
35088
12
4
–
164
8
5632
9
1
24
#S OL
–
22–40
18–32
12–64
11–29
11–23
14–27
13–14
7–18
8–27
9–9
8–8
–
23–29
12–19
10–23
11–12
5–5
12–13
QC
5
5
5
4
5
5
5
4
4
5
3
2
–
8
6
7
5
5
5
d
124.04
108.39
97.95
1.65
0.09
0.09
0.09
0.04
0.89
36.32
0.04
0.02
>3000.00
64.83
9.60
250.81
0.03
7.19
1.50
T IME
MCT+MCF+P
431
402
198
38
513
911
913
8
768
58176
12
4
–
1084
8
5856
43
1
24
#S OL
17–34
17–39
17–33
16–30
11–31
11–31
14–29
13–16
7–18
8–39
9–9
8–8
–
23–33
12–19
10–25
11–20
5–5
12–13
QC
90 4 Exact Synthesis of Reversible Logic
4.4 Summary and Future Work
91
4.4 Summary and Future Work Even if they are only applicable to small functions, exact synthesis methods are important in the context of evaluating heuristic methods, determining minimal building blocks (e.g. for the BDD-based synthesis as introduced in Sect. 3.2), and other aspects as well. In this chapter, several approaches based on satisfiability techniques have been introduced enabling exact synthesis for functions with up to six variables and leading to circuits with up to twelve gates. A comparison with the results obtained by previous approaches showed: Smaller circuits than the currently best known ones have been synthesized or, for the first time, the minimality of existing circuits was approved. Furthermore, it was shown that choosing an encoding for exact Toffoli circuit synthesis is crucial to the resulting run-times. Lifting the originally proposed Boolean SAT encoding to the SMT- or a problem-specific level accelerates the synthesis time by three or four orders of magnitudes, respectively. Complementary, applying quantifiers together with BDDs, a further speed-up by more than a factor of 100 can be observed in the best case. By the approaches proposed in this chapter, the number of reversible gates as well as the number of quantum gates (and therewith also quantum cost) have been considered. Depending on the addressed physical realization, further constraints (e.g. concurrent gates, adjacent gates, etc.) are important as well. Thus, exact synthesis with respect to further cost criteria might be a topic for future work. Chapter 6 discusses this aspect in detail and also proposes one respective exact approach for this aim. The results of this chapter build the basis for future investigations. The QBF encoding leads to the best results (in particular, with respect to run-time), since it does not need the exponential duplication of the instance for each truth table line. However, this encoding is still on the Boolean level, since a BDD is used as underlying solving engine. Thus, lifting the quantified encoding to higher levels of abstraction (as done for the Boolean SAT encoding) would be a promising task for future work. Therefore, respective solving engines efficiently supporting quantifiers must be available first. Additionally, the encoding itself is improvable. So far, all possible gate type combinations are tried by the solving engine. But, many combinations are redundant and can be ignored. Identifying easy to detect redundancies and exclude those from the search space may accelerate the solving process. Also, special function classes (e.g. symmetric functions) probably can be synthesized faster if the respective properties are fully exploited in the encoding. Furthermore, the application of advanced solving techniques like incremental SAT (see e.g. [WKS01]) seems to be promising, since several iterations of very similar instances are sequentially solved in the proposed synthesis approach.
Chapter 5
Embedding of Irreversible Functions
Quite often reversible logic should be synthesized for irreversible functions. Thus, the problem of embedding is an important aspect. Partially, how to handle irreversible functions during synthesis has already been discussed in the previous chapters (see e.g. Sects. 3.1.1 and 4.2.3). Additional lines are thereby introduced and the resulting constant inputs, garbage outputs, and don’t care conditions are arbitrarily assigned to concrete values. Further options exist how (i.e. in which order) to arrange the outputs in the circuit to be synthesized. Overall, functions can be embedded in different ways whereby the concrete don’t care assignments as well as the chosen output arrangement may have a significant impact on the resulting circuit size. As an example, for BDD-based synthesis introduced in Sect. 3.2 different output orders are applied to the building block-functions as they lead to better substitutions of the respective nodes. Since in the last chapters synthesis approaches (in particular the transformation-based approach and the exact synthesis method) have been described, now they can be used to evaluate the effect of different embeddings. In this chapter, the different aspects of embedding mentioned above are investigated in detail. First, strategies for the don’t care assignment [MDW09, MWD09] are proposed. More precisely, a greedy approach, a method based on the Hungarian algorithm, and an XOR-based strategy are introduced. Even if these strategies address don’t care assignments of the outputs only, it can be shown that the chosen method is crucial to the synthesis results. Afterwards, the order of outputs in the function to be synthesized is considered. Usually, each output is set to a fix position. But since in general, the output order is irrelevant for a given reversible function f , a new synthesis paradigm [WGDD09] is proposed that determines an equivalent circuit realization for f modulo the output permutation. That is, the result of the synthesis is a circuit whose outputs have been permuted. Therefore, distinct methods to efficiently determine “good” output permutations are introduced. As a result, significantly smaller circuits (even smaller than the ones previously obtained by the exact approaches) can be synthesized if this new synthesis paradigm is applied. In the following, the embedding problem together with the number of possibilities is described in Sect. 5.1, which therewith builds the motivation for the remaining sections. Afterwards, the approaches for don’t care determination (Sect. 5.2) R. Wille, R. Drechsler, Towards a Design Flow for Reversible Logic, DOI 10.1007/978-90-481-9579-4_5, © Springer Science+Business Media B.V. 2010
93
94
5 Embedding of Irreversible Functions
Table 5.1 Embedding of an adder (b) Incomplete embedding (a) Original adder
(c) Complete embedding
cin x y
cout sum
0 cin x y
cout sum g1 g2
0 cin x y
cout sum g1 g2
0 00
0
0
0 0
0 0
0
0
–
–
0 0
0 0
0
0
0
0
0 01
0
1
0 0
0 1
0
1
–
–
0 0
0 1
0
1
0
0
0 10
0
1
0 0
1 0
0
1
–
–
0 0
1 0
0
1
0
1
0 11
1
0
0 0
1 1
1
0
–
–
0 0
1 1
1
0
0
0
1 00
0
1
0 1
0 0
0
1
–
–
0 1
0 0
0
1
1
0
1 01
1
0
0 1
0 1
1
0
–
–
0 1
0 1
1
0
0
1
1 10
1
0
0 1
1 0
1
0
–
–
0 1
1 0
1
0
1
0
1 11
1
1
0 1
1 1
1
1
–
–
0 1
1 1
1
1
0
0
1 0
0 0
–
–
–
–
1 0
0 0
0
0
0
1
1 0
0 1
–
–
–
–
1 0
0 1
0
0
1
0
1 0
1 0
–
–
–
–
1 0
1 0
0
0
1
1
1 0
1 1
–
–
–
–
1 0
1 1
0
1
1
1
1 1
0 0
–
–
–
–
1 1
0 0
1
0
1
1
1 1
0 1
–
–
–
–
1 1
0 1
1
1
0
1
1 1
1 0
–
–
–
–
1 1
1 0
1
1
1
0
1 1
1 1
–
–
–
–
1 1
1 1
1
1
1
1
and output permutation (Sect. 5.3) are proposed and evaluated. At the end of this chapter, all results are summarized and future work is sketched.
5.1 The Embedding Problem As already described in Sect. 3.1.1, at least g = log2 (μ) additional outputs are required to embed a completely specified irreversible function into a reversible function, where μ is the maximum number of times an output pattern is repeated in the irreversible function. For an irreversible function f : Bn → Bm , this means that the reversible embedding has m + g outputs. Furthermore, c constant inputs must be added such that n + c = m + g. Once the garbage outputs and constant inputs are added, an open issue is how to assign the don’t care conditions in the expanded truth table as shown by the following example. Example 5.1 Consider the adder function shown in Table 5.1(a). This function has three inputs (the carry-in cin as well as the two summands x and y) and two outputs (the carry-out cout and the sum). The function is irreversible, because the number of inputs differs from the number of outputs. Since the output pattern 01 appears three times (as does the output pattern 10), adding one additional output (leading to the same number of input and outputs) cannot make the function reversible. In
5.1 The Embedding Problem
95
Fig. 5.1 Circuits obtained with different embeddings
fact, log2 (3) = 2 additional outputs (and therewith one constant input) must be added. This is shown in Table 5.1(b). But since this incompletely specified function is not applicable for many synthesis approaches, afterwards the don’t cares must be assigned. One possible, albeit naive, embedding is shown in Table 5.1(c). This embedding was found by assigning the garbage outputs to the patterns 00, 01, and 10 in order for each of the output patterns in the top half of the table and then completing the bottom half of the table using the remaining available output patterns in numerical order. Remark 5.1 Not every synthesis approach requires a completely specified reversible function. For example, the SAT-based approach introduced in the last chapter can also handle don’t cares (see Sect. 4.2.3). However, most of the other synthesis approaches (as e.g. [Ker04, GAJ06, MDM07] and the transformation-based method described in Sect. 3.1.2) need a completely specified function. For these approaches, a completely specified embedding is required. To appreciate the complexity of choosing a don’t care assignment, consider Table 5.1(b). There are 4, 4, 3, 4, 2, 3, 2, and 4 choices for completing the don’t cares in the top eight rows of the table, respectively for a total of 9216 choices. The bottom eight rows of the table can then be completed in 8! ways. Lastly, the outputs can be permuted in 4! = 24 ways. Combining these yields 9216 · 40320 · 24 = 8, 918, 138, 880 possible embeddings for this small example. Each respective embedding may have an effect on the synthesis results, i.e. on the size of the resulting circuits. For example, the embedding from Table 5.1(c) lead to the circuit depicted in Fig. 5.1(a) (obtained by the transformation-based approach from Sect. 3.1.2). In contrast, using the embedding introduced in Sect. 3.1.1 (see Table 3.2 on p. 29), a significantly smaller circuit result as shown in Fig. 5.1(b). Thus, in the next two sections strategies how to determine “good” don’t care assignments and output permutations, respectively, are introduced and evaluated.
96
5 Embedding of Irreversible Functions
5.2 Don’t Care Assignment In this section, methods for don’t care assignment are presented and evaluated, respectively, that complete a reversible embedding of an irreversible function. It is assumed that always the minimal number of outputs (i.e. log2 (μ)) is added. Furthermore, all constant inputs are assigned to the value 0 and are always added as the most significant inputs in the truth table. This leads to a significant computational advantage as shown below and results in a circuit overhead of at most one NOT gate per constant input.
5.2.1 Methods In total, three approaches are introduced: A greedy algorithm, a method based on the Hungarian algorithm, and an XOR-based procedure. Afterwards, an incomplete application of the don’t care assignment methods is discussed that can be applied to many existing synthesis approaches and leads to a significant simplification of the synthesis process.
5.2.1.1 Greedy Method The first method for assigning don’t cares is motivated by the basic operation of the transformation-based synthesis algorithms (see Sect. 3.1.2). Here, gates are chosen so that each input value of the truth table matches its respective output value (i.e. so that the identity is achieved). Each line of the truth table is thereby sequentially traversed. It is thus reasonable to conjecture that assigning the don’t cares so that the Hamming distance of the output patterns to the corresponding input patterns is as small as possible, should help to reduce the number of gates required. This firstly leads to a simple greedy approach. The truth table is traversed downwards starting at the first row. In each row, the following two steps are performed: 1. For each distinct output assignment in the embedding, identify the target set of rows of the table containing that pattern. Then, determine the set of output assignments which are found by assigning the don’t cares in all possible ways. The candidates are arranged in ascending numerical order. 2. For each row in the target set in turn, choose the first remaining candidate assignment with minimal Hamming distance to the input assignment for that row. Example 5.2 Table 5.2(a) shows the embedding obtained by the greedy method for the full adder. The circuits synthesized from this assignment as well as from the naive assignment given in Table 5.1(c) are shown in Fig. 5.2(a) and Fig. 5.2(c),
5.2 Don’t Care Assignment
97
Table 5.2 Resulting embeddings of an adder after don’t care assignment (b) XOR-based method (a) Greedy/Hungarian method 0
cin
x
y
cout
sum
g1
g2
0
cin
x
y
cout
sum
g1
g2
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
1
0
1
0
0
0
1
0
1
1
1
0
0
1
0
0
1
1
0
0
0
1
0
0
1
1
0
0
0
1
1
1
0
1
1
0
0
1
1
1
0
0
1
0
1
0
0
0
1
0
0
0
1
0
0
0
1
0
0
0
1
0
1
1
0
0
1
0
1
0
1
1
0
1
1
0
1
1
0
1
0
1
0
0
1
1
0
1
0
1
0
0
1
1
1
1
1
1
1
0
1
1
1
1
1
0
1
1
0
0
0
1
0
0
0
1
0
0
0
1
0
0
0
1
0
0
1
0
0
0
1
1
0
0
1
1
1
1
1
1
0
1
0
0
0
1
0
1
0
1
0
1
1
1
0
1
0
1
1
0
0
1
1
1
0
1
1
0
0
0
1
1
1
0
0
1
1
0
0
1
1
0
0
1
1
0
0
1
1
0
1
1
1
0
1
1
1
0
1
0
0
1
1
1
1
1
0
1
1
1
0
1
1
1
0
0
0
1
0
1
1
1
1
0
1
1
1
1
1
1
1
0
1
0
1
Fig. 5.2 Circuits for the embeddings
98
5 Embedding of Irreversible Functions
respectively.1 The greedy assignment method leads to a circuit with 7 gates and quantum cost of 27, while with the naive embedding a circuit with 20 gates and quantum cost of 44 results.
5.2.1.2 Applying the Hungarian Algorithm Furthermore, the Hamming distance can be applied to formulate the don’t care assigning problem as an instance of the Hungarian algorithm [HL05]. To this end, let S be the set of truth table rows sharing a common output pattern in the irreversible function and let T be the set of possible assignments to the don’t cares to complete those rows. |T | is equal to 2g where g is the number of garbage lines added to permit the embedding of the irreversible function into a reversible one. Then, the don’t care assignment problem is to associate each element of S with a unique element from T . Let K(Si , Tj ) be the “cost” of associating the don’t care assignment Tj with Si , for which the hamming distance is applied. More precisely, K(Si , Tj ) is the Hamming distance between the completely specified truth table output pattern and the corresponding input pattern when Si is completed using Tj . This formulation can be expressed in tabular form with a row for each Si and a column for each Tj with each K(Si , Tj ) in the corresponding table entry. Assigning the don’t cares to minimize the total Hamming distance is then a matter of choosing one entry in each row such that those entries appear in unique columns and such that the sum of the chosen entries is minimal. This is a standard assignment problem. The Hungarian algorithm is a well-known method [HL05] for solving the assignment problem in polynomial time and thus has been applied to solve this instance. The only issue of note here is that storing the potentially very large assignment matrix is avoided, since Hamming distance is easily computed as needed—in fact more quickly than a matrix access. Example 5.3 Applying the Hungarian algorithm to the considered adder function, the same assignment as for the greedy method results. This may happen since both approaches use the Hamming distance as cost metric. Nevertheless, the experiments in Sect. 5.2.2 show that both assignment methods lead to notable differences for other (mainly larger) functions.
5.2.1.3 XOR-based Method The third method proposed for don’t care assignments is based on the observation that for many functions (in particular for arithmetic) a good embedding of an irreversible function into a reversible one is based upon setting the don’t care outputs to XOR combinations of the primary inputs. More precisely, the following steps are performed: 1 The
transformation-based synthesis method from Sect. 3.1.2 has been used to synthesize these circuits.
5.2 Don’t Care Assignment
99
1. For each truth table row i of the embedding f : Bn → Bn (i.e. 0 ≤ i < 2n ): a. Set k = i so that k represents the current input vector as a natural number. b. Set p = 0, q = 0. c. For each output fj of the embedding (i.e. 0 ≤ j < n): i. If fj is a garbage output, set q = q ⊕ kj and pj = q with kj (pj ) denoting the j th bit-value of the binary encoding of k (p). ii. Otherwise set pj to the j th bit-value of fi . In doing so, an output assignment is created (represented by the natural number p), where the don’t cares are assigned to a value achieved by an XORcombination with the respective input values. d. If p represents an already assigned output pattern, increment k and repeat from Step 1b. e. Set the output of the ith truth table line to p. Example 5.4 Table 5.2(b) shows the embedding obtained by the XOR-based method for the full adder. The circuit obtained from this assignment is shown in Fig. 5.2(b) (also synthesized using the transformation-based synthesis method). The XOR-based method yields a circuit with five gates and quantum costs of 13, which is significantly smaller than the circuits obtained with the greedy/Hungarian and the naive embedding, respectively. Overall, these circuits clearly show the importance of a good don’t care assignment. 5.2.1.4 Incomplete Don’t Care Assignment As noted above, all constant inputs take the value 0 and are always the most significant inputs in the truth table. This means that the irreversible function is always embedded in the first rows of the reversible truth table, while the remaining rows are completely don’t care (see e.g. Table 5.1(c)). In particular, if the original function has n primary inputs and, furthermore, c constant inputs are added, only the first 2n truth table rows of the embedding are of interest. The remaining (2c − 1)2n rows can be ignored. Given this construction, a synthesis method that works row by row from the top of the truth table (as e.g. the transformation-based synthesis approach from Sect. 3.1.2 and its derivates) can stop after transforming 2n rows. Because of this, it is not necessary to complete a don’t care assignment beyond the (2n )th row.2
5.2.2 Experimental Results In this section, experimental results obtained with the described don’t care assigning approaches are documented. To this end, (irreversible) functions from 2 Note that when this simplification is employed, bidirectional synthesis methods cannot be applied,
because don’t cares occur in the latter truth table lines so that no definition of the inverse function is possible.
100
5 Embedding of Irreversible Functions
RevLib [WGT+08] have been embedded using the proposed methods. Afterwards, the resulting embeddings have been passed to (1) the transformation-based synthesis from Sect. 3.1.2 (denoted by T RANSFORMATION - BASED A LGORITHM) or (2) to an extended version combining transformation-based synthesis with a searchbased method as proposed in [MWD09] (denoted by C OMBINED S YNTHESIS A L GORITHM ), respectively. An AMD Athlon 3500+ with 1 GB of memory was used for the experiments. Table 5.3 presents the results for each of the synthesis approaches and don’t care assignment methods, respectively. For the resulting circuits, the gate count (denoted by d) and the quantum cost (denoted by QC) are shown. Furthermore, for each function, the best result with respect to quantum cost is highlighted in bold. Run-times are not documented, since every circuit in Table 5.3 was found in less than one CPU second. The results show that the chosen embedding is crucial to the synthesis results. For example, the quantum costs of the circuits representing function rd73_69 range from 1112 to 184 using the combined synthesis approach. Thus, an improvement of nearly one order of magnitude can be achieved only by modifying the assignment of the don’t cares. In the next section, the second aspect of embedding, namely permutation of outputs, is considered in detail, where similar results have been achieved.
5.3 Synthesis with Output Permutation Usually, the outputs of a reversible function (or embedding, respectively) to be synthesized are set to a fix position. Since in general the output order is irrelevant for a given reversible function f , in this section a synthesis methodology—emphasized as Synthesis with Output Permutation (SWOP)—is proposed. SWOP determines a circuit for the function f modulo output permutation. That is, the result is a circuit representing the desired function but maybe with an adjusted order of outputs. This enables the determination of smaller realizations. In a naive way, synthesis with output permutation can easily be applied to existing approaches just by encoding all permutations, synthesize each in one turn, and keep the best one. Since each respective output order has to be considered, this results in an increase of factor n! (where n is the number of variables of the reversible n! function). If garbage outputs occur, this complexity can be reduced to g! (where g is the number of garbage outputs), which might still be a large number. Thus, in this section two approaches are introduced that efficiently determine “good” output permutations for both, exact as well as heuristic synthesis approaches. Moreover, applying the proposed exact synthesis with output permutation, also minimality with respect to all possible permutations is guaranteed. In the remainder of this section, both approaches are described in detail. First, the general idea, the best case benefits, as well as the complexity of the proposed synthesis paradigm are introduced and discussed in Sect. 5.3.1. Afterwards, exact SWOP and heuristic SWOP are described in Sects. 5.3.2 and 5.3.3, respectively. Finally, experimental results are given in Sect. 5.3.4.
9
10
11
sym9_71
rd84_70
5
mini-alu_84
rd73_69
5
alu_9
7
5
4mod7_26
7
5
4mod5_8
sym6_63
5
4gt5_21
rd53_68
2
5
4gt4_20
5
5
4gt13_25
6
5
4gt12_24
one-two-three_27
5
4gt11_23
decod24-enable_32
1
5
4gt10_22
3
1
2
1
2
3
0
1
1
1
1
1
1
1
1
1
4
2
4
d
rd32_19
n
decod24_10
F UNCTION
7
9
6
6
4
2
2
3
4
2
4
4
4
4
4
4
4
2
0
g
104
76
80
36
27
15
9
22
9
21
7
3
6
1
3
1
3
5
7
1823
1047
1112
485
228
39
33
110
65
65
19
19
58
13
55
5
47
13
11
7
3
6
1
3
1
3
5
7
104
76
80
36
27
11
9
26
16
21
d
1823
1047
1112
485
228
35
33
114
72
65
19
19
58
13
55
5
47
13
11
QC
H UNGARIAN
XOR- BASED
47
51
40
17
22
14
9
25
28
21
5
6
9
3
6
4
4
5
7
d
446
573
184
133
137
42
33
125
224
65
9
30
65
27
62
12
40
13
11
QC
9
3
7
1
3
1
3
5
7
111
210
100
36
22
15
9
22
12
15
d
2100
4368
2187
777
187
39
33
98
64
55
25
19
79
13
55
5
47
17
11
QC
9
3
7
1
3
1
3
5
7
111
210
100
36
22
13
9
28
16
15
d
2100
4368
2187
777
187
37
33
108
68
55
25
19
79
13
55
5
47
17
11
QC
H UNGARIAN
7
9
3
7
1
3
1
3
5
111
210
100
36
22
15
9
22
32
15
d
2100
4368
2187
777
187
39
33
110
252
55
25
19
79
13
55
5
47
17
11
QC
XOR- BASED
G REEDY
QC
G REEDY
d
T RANSFORMATION - BASED A LGORITHM
C OMBINED S YNTHESIS A LGORITHM
Table 5.3 Comparison of don’t care assignment methods
5.3 Synthesis with Output Permutation 101
102 Table 5.4 Function specification
5 Embedding of Irreversible Functions x1
x2
x3
f1
f2
f3
0
0
0
0
0
0
0
0
1
0
1
0
0
1
0
1
0
0
0
1
1
1
1
1
1
0
0
0
0
1
1
0
1
0
1
1
1
1
0
1
0
1
1
1
1
1
1
0
Fig. 5.3 Minimal Toffoli circuits
5.3.1 General Idea Many synthesis approaches get a reversible function (or a reversible embedding, respectively) f : Bn → Bn , where each specified output has a fix position. Thus, often only a fix order of outputs is considered during the synthesis. Example 5.5 Consider the function specification shown in Table 5.4. The reversible function maps (x1 , x2 , x3 ) to (x2 , x3 , x2 x3 ⊕ x1 ) = (f1 , f2 , f3 ). A minimal Toffoli circuit for this function is shown in Fig. 5.3(a). This circuit consists of 6 gates. Usually, the order of the outputs is irrelevant and can be swapped. As shown in the following example, this can lead to a much more compact circuit. Example 5.6 In Fig. 5.3(b) a Toffoli circuit is depicted which computes the same reversible function than the Toffoli circuit shown in Fig. 5.3(a). But in contrast, the three output functions have been reordered to another position in the output vector. More precisely, the Toffoli circuit shown in Fig. 5.3(b) maps the input (x1 , x2 , x3 ) to the output (x2 x3 ⊕ x1 , x2 , x3 ) = (f3 , f1 , f2 ). This reduces the overall number of gates from 6 to 1, i.e. 5 gates have been saved. Motivated by this example, a new synthesis paradigm denoted as Synthesis with Output Permutation (SWOP) is introduced. To this end, synthesis approaches are ex-
5.3 Synthesis with Output Permutation
103
Fig. 5.4 Permutations with garbage outputs
Fig. 5.5 Realization of a permutation
tended in such a way that different (or all) output permutations are considered. This causes a significant increase in complexity, since in general all possible permutations have to be checked (resulting in n! different synthesis calls in total). This can be slightly reduced, if a function containing garbage outputs should be synthesized. n! different permutations have to be considered, since permutations of Then, only g! the garbage outputs can be ignored. Example 5.7 Figure 5.4 shows all n! possible permutations for function with n = 3 variables and g = 2 garbage outputs (denoted by g1 and g2 ). Since the garbage outputs are left unspecified, the permutations that only swap garbage outputs can be skipped (i.e. the last three permutations of Fig. 5.4). Thus, only 3! 2! = 3 permutations instead of all 3! = 6 permutations are considered. Nevertheless, the number of additional checks is quite high. In contrast, synthesis with output permutation may lead to significant reductions in the resulting circuit sizes. To illustrate this, consider Fig. 5.5 depicting the gates needed to permute two signals in a reversible circuit with Toffoli gates (in total three gates are required). Since the best position of the outputs is unknown at the beginning of the synthesis process, outputs may be placed arbitrarily in the function specification. Then, the three gates of Fig. 5.5 may be needed to permute the value of a signal to the position given by the function. If in contrast output permutation is considered during the synthesis, the number of gates of the resulting circuit may be significantly lower. Lemma 5.1 The number of gates in a reversible circuit obtained by common synthesis approaches may be up to 3 · (n − 1) higher than the number of gates in a circuit where synthesis with output permutation is applied (with n being the number of variables). Proof Let d be the minimal number of gates of a circuit obtained by enabling output permutation during synthesis. To move one output line to the position given by the function three Toffoli gates are required (see Fig. 5.5). At most n − 1 lines need to be moved. It follows that the cost of the minimal circuit, where no output permutation is allowed, is less than or equal to d + 3 · (n − 1).
104
5 Embedding of Irreversible Functions
Remark 5.2 Lemma 5.1 gives a best case improvement. Because of the heuristic nature of most of the synthesis approaches, of course also circuits with a larger number of gates may result. This motivates the investigation of methods that exploit output permutation during the synthesis. The next two sections show how this is realized using an exact synthesis approach as well as a heuristic synthesis approach, respectively.
5.3.2 Exact Approach As intensely considered in Chap. 4, exact synthesis approaches determine minimal circuits for a given function, i.e. circuits with the minimal number of gates. The methods introduced in Chap. 4 exploit Boolean satisfiability (SAT) techniques where the basic idea is to check whether there exists a Toffoli circuit for a reversible function with d gates (starting with d = 1, where d is increased in each iteration if no realization is found). The respective checks are performed by representing the problem as an instance of SAT which is afterwards solved by a SAT solver or similar (specialized) solving engines (for more details see the respective sections in Chap. 4). To describe how synthesis with output permutation is applied to this exact synthesis method, the concrete SAT encoding is sketched as follows: Definition 5.1 Let f : Bn → Bn be a reversible function to be synthesized. Then, the SAT instance of the respective synthesis problem is given as Φ∧
n −1 2
([inpi ]2 = i ∧ [outi ]2 = f (i)),
i=0
where • inpi is a Boolean vector representing the inputs of the circuit to be synthesized for truth table line i, • outi is a Boolean vector representing the outputs of the circuit to be synthesized for truth table line i, and • Φ is a set of constraints representing the synthesis problem as described in Sects. 4.2.1 and 4.3.1, respectively. As an example Fig. 5.6(a) shows the simplified representation of the synthesis problem for the function specified in Table 5.4 (where the values of the truth table are given as integers). Applying SWOP to the exact approach and still ensuring minimality, all pern! mutations have to be considered. This can be done—as mentioned above—by g! separate synthesis calls. However, exploiting the advanced techniques of the used SAT solvers leads to faster synthesis. Therefore, an adjusted encoding is proposed which requires one additional Boolean vector.
5.3 Synthesis with Output Permutation
105
Fig. 5.6 Encoding for exact synthesis
Definition 5.2 Let f : Bn → Bn be a reversible function to be synthesized. Then, p = (plog n! , . . . , p1 ) is a Boolean vector representing the binary encoding of a 2 g!
n! natural number p ∈ {1, . . . , g! } which indicates the chosen output permutation of the circuit.
Using this vector, the SAT encoding is slightly extended: According to the assignments to p (set by the SAT solver), a value for p is determined, which selects the current output permutation. Depending on this permutation the respective output order is set during the search. More formally, the encoding of Definition 5.1 is extended as follows: Φ∧
n −1 2
([inpi ]2 = i ∧ [outi ]2 = πp (f (i))).
i=0
The extended encoding of the synthesis problem for the function specified in Table 5.4 is illustrated in Fig. 5.6(b). If the solver finds a satisfying assignment for this SWOP instance, one can obtain the circuit from the result as described in Chap. 4 and the best permutation is provided by the assignment to p. Overall, this extension allows exact SWOP with only one synthesis call in conn! separate ones. Furthermore, since the variables of p are an integral part of trast to g! the search space, the permutations are checked much more efficiently. Because of modern SAT techniques (in particular conflict analysis [MS99]), during the search process reasons for conflicts are learned. This learned information prevents the solver from reentering non-solution search space, i.e. large parts of the search space are pruned. In contrast, this information is not available when each permutation is checked by separate calls of the solver. Thus, exact synthesis with output permutation is possible in feasible run-time when learning is exploited.
5.3.3 Heuristic Approach To apply SWOP to a heuristic approach, the transformation-based algorithm described in Sect. 3.1.2 is considered. To avoid the construction of all possible permutations (which would lead to a complexity increase of n!, since the transformationbased approach does not support garbage outputs), a SWOP-based synthesis heuristic using a sifting algorithm is proposed. This algorithm is inspired by [Rud93] and
106 Fig. 5.7 Heuristic SWOP
5 Embedding of Irreversible Functions
(1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (11) (12)
HeuristicSWOP(f : Bn → Bn ) // f is given in terms of a truth table perm = (1, 2, . . . , n); dbest = synthesize(perm); best_perm = perm; for i = 0 to n − 2 do for j = i + 1 to n − 1 do tmp_perm = swap(perm, i, j ); dtmp = synthesize(tmp_perm); if (dtmp < dbest ) best_perm = tmp_perm; perm = best_perm;
reduces the above mentioned complexity to n2 . Because of the heuristic behavior of sifting, maybe not the best permutation is determined. However, as the experiments in Sect. 5.3.4 show, significant improvements can be achieved in feasible run-times. The pseudo-code for the sifting algorithm is given in Fig. 5.7. First, an initial permutation (given by the function) is chosen and the circuit for this function is synthesized (line 3 and line 4). The gate count of this first realization is stored. After this, for each output the best position within the current permutation is searched. This is done by swapping the position of the current output with each of the other positions leading to new permutations (line 8). For each of these new permutations, the respective circuit is synthesized (line 9). If the gate count of such a circuit is smaller than the current best known gate count (line 10), the current permutation is stored as being the best one (line 11). When each position for one output has been checked, the best permutation of these checks is used for the remaining outputs (line 12). In summary, for each of the first n − 1 outputs, the algorithm will find a new position that will result in a realization with the fewest gates—when synthesized with the transformation-based approach. Therewith the complexity of SWOP can be reduced while still improving the obtained results as the next section will show.
5.3.4 Experimental Results This section provides experimental results for SWOP. In total, four different aspects are studied: (1) the reduction of the complexity of SWOP when garbage outputs are considered, (2) the results of exact SWOP in comparison to the common exact synthesis, (3) the results of heuristic SWOP in comparison to the common transformation-based approach, and (4) the quality (with respect to the number of gates) of the circuits synthesized by SWOP in comparison to the currently best known realizations. For exact synthesis, the SWORD approach introduced in Sect. 4.3.1 has been used. The SWOP extension was implemented on top of this approach. As heuristic approach, the transformation-based approach (including template matching as described in [MDM05]) was applied. The respective benchmark functions have been
5.3 Synthesis with Output Permutation
107
Table 5.5 SWOP considering garbage outputs SWOP
F UNCTION n
g
d
n!
OPT. SWOP T IME
n! g!
T IME
I MPR
4mod5
5
4
5
120
233.18
5
7.37
decod24
4
0
5
24
0.10
24
0.10
31.6 1.0
gt4
4
3
3
24
<0.01
4
<0.01
1.0
gt5
4
3
1
24
0.01
4
<0.01
>1.0
low-high
4
3
4
24
3.71
4
0.39
9.51
zero-one-two
4
1
4
24
0.03
24
0.02
1.5
maj4_1
5
4
6
120
3500.90
5
2125.62
1.6
maj4_2
5
4
5
120
191.92
5
4.19
45.8
alu
5
4
6
120
2013.72
5
61.24
32.9
mini_alu_1
4
2
5
24
0.28
12
0.19
1.5
mini_alu_2
5
3
7
120
930.60
20
474.42
1.9
mini_alu_3
5
3
5
120
9.60
20
2.07
4.6
taken from RevLib [WGT+08]. All experiments have been carried out on an AMD Athlon 3500+ with 1 GB of main memory. All run-times are given in CPU seconds. The timeout was set to 3600 CPU seconds. 5.3.4.1 SWOP with Garbage Outputs In a first series of experiments the different complexities are compared which occur if Toffoli circuits for functions containing garbage outputs are synthesized. Here, n! permutations are considered. instead of n! permutations, only g! Table 5.5 shows the results of the exact SWOP approach with n! permutations n! permutations for functions containing garbage outputs. The first three and with g! columns provide the name of the function, the number of circuit lines n, and the number of garbage outputs g, respectively. The minimal number of gates of the obtained Toffoli circuits is given in column d. Then, the run-times of SWOP with n! n! permutations are given, respectively (denoted by T IME). Furthermore, and with g! n! the improvement of the optimized SWOP (i.e. the synthesis with only g! permutations) in comparison to SWOP with all n! permutation is provided (i.e. run-time of SWOP divided by run-time of OPT. SWOP). As expected the reduction of permutations leads to better run-times for all functions. Improvements of up to a factor of 45 can be achieved in the best case.
5.3.4.2 Exact SWOP In this section, exact SWOP is compared to the exact approach from Sect. 4.3.1. The results are shown in Table 5.6. Here again, the first column provides the name
108
5 Embedding of Irreversible Functions
Table 5.6 Comparison of exact synthesis to exact SWOP E XACT S YNTHESIS
F UNCTION n
g
d
T IME
SWOP-T IME S YN -T IME
E XACT SWOP d
vs.
n! g!
T IME
4mod5
5
4
5
0.9
5
7.4
8.2 > 5
decod24
4
0
6
0.1
5
0.1
1.0 < 24
gt4
4
3
4
<0.1
3
<0.1
gt5
4
3
3
<0.1
1
0.1
1.0 < 4
low-high
4
3
5
0.2
4
0.4
2.0 < 4
zero-one-two
4
1
5
<0.1
4
<0.1
maj4_1
5
4
6
438.0
6
2125.6
1.0 < 4
1.0 < 24 4.9 < 5
maj4_2
5
4
6
13.6
5
4.2
0.3 < 5
alu
5
4
7
423.3
6
61.2
0.1 < 5
mini_alu_1
4
2
5
<0.1
5
0.2
2.0 < 12
mini_alu_2
5
3
8
2460.0
7
474.4
0.2 < 20
mini_alu_3
5
3
5
0.2
5
2.1
10.5 < 20
3_17
3
0
6
<0.1
5
<0.1
1.0 > 6
graycode6
6
0
5
<0.1
5
13.5
13.5 < 720
mod5d1
5
0
7
11.8
7
184.1
15.6 < 120
mod5d2
5
0
8
9.9
8
1097.6
110.8 < 120
mod5mils
5
0
5
0.1
5
1.7
17.0 < 120
of the function, while n and g denote the number of variables and the number of garbage outputs, respectively. The next columns give the minimal number d of gates determined by the two approaches and the corresponding run-times. The last column shows information relating to the complexity, i.e. the run-time overhead when output -Time n! permutation is considered ( SWOP Syn-Time ) compared to the factor g! . It can be seen that for many functions, SWOP found smaller circuits than the ones generated by the previous exact synthesis approach. Thus, removing the restriction for the output order leads to smaller circuits for many of the well known benchmark functions. As expected, the run-time for SWOP is higher in comparison to the run-time of the pure exact synthesis. This is, because the search space is obviously larger due to the number of output permutations that can be chosen. However, the increase is n! not as high as the worst case complexity ( g! ). This can be seen in the last column of Table 5.6. For all benchmarks (except 4mod5 and 3_17) the run-time of SWOP divided by the run-time of the previous synthesis approach is significantly smaller n! than g! . As explained, this is due to search space pruning, possible if the encoding is extended so that all permutations are checked in parallel. Moreover, for some benchmarks (e.g. maj4_2 or alu) the run-time of SWOP is even smaller than for a single exact solution. This reduction is caused by the fact that smaller circuits are found and thus the synthesis terminates earlier.
5.3 Synthesis with Output Permutation
109
5.3.4.3 Heuristic SWOP The results obtained by common heuristic synthesis (i.e. by the transformationbased approach including templates [MDM05]) are given in Table 5.7. More precisely, the gate counts of the resulting circuits as well as the run-time needed for their synthesis is given for (1) the original algorithm (denoted by H EURISTIC S YN THESIS ), (2) for the SWOP-based synthesis where all permutations are considered (denoted by ALL PERMS), and (3) for the SWOP-based synthesis where the sifting algorithm introduced in Sect. 5.3.3 is used (denoted by S IFTING). As can clearly be seen, the effect of output permutation is significant for most of the functions. For example, for the function aj-e13 the realization is reduced by 30 percent from 40 gates to 28 gates. The best absolute reduction of gates can be obTable 5.7 Comparison of heuristic synthesis to heuristic SWOP F UNCTION
H EURISTIC SWOP
H EURISTIC S YNTHESIS n
3_17
3
6
4_49
4
4mod5
5
S IFTING
ALL PERMS
T IME
d
T IME
d
0.03
6–7
17
0.40
9
0.03
T IME
d
0.32
6
0.25
14–22
4.09
16
1.09
9–21
10.02
9
0.75
5mod5
6
18
0.13
14–37
254.14
18
3.59
aj-e10
5
33
0.63
22–51
107.03
30
8.21
aj-e11
4
12
0.09
11–22
2.46
11
0.55
aj-e12
5
26
0.35
25–57
103.37
25
8.11
aj-e13
5
40
0.97
28–51
112.70
34
12.31
ex1
3
4
<0.1
0.08
4
0.06
<0.1
graycode3
3
2
graycode4
4
3
0.01
4–8 2–5
0.01
2
0.01
3–9
0.32
3
0.07
graycode5
5
4
0.03
4–13
4.72
4
0.31
graycode6
6
5
0.08
5–18
67.25
5
1.08
hwb3
3
7
0.06
6–11
0.32
7
0.29
hwb4
4
15
0.35
10–21
3.70
10
0.69
hwb5
5
55
1.66
38–62
153.71
44
16.49
prime5
6
15
0.20
13–40
227.05
13
3.09
prime5a
6
16
0.10
14–41
291.58
14
3.92
ham3
3
5
0.01
3–5
0.02
4
0.03
hwb6
6
125
7.08
–
>3600.00
91
89.20
hwb7
7
283
33.26
–
>3600.00
259
656.82
hwb8
8
676
152.13
–
>3600.00
641
4525.22
ham7
7
23
0.34
–
>3600.00
23
49.47
rd53
7
16
0.26
–
>3600.00
13
10.04
110 Table 5.8 Best results obtained by SWOP
5 Embedding of Irreversible Functions F UNCTION
B EST KNOWN d
SWOP d
Δd
decod24
6
5
1
gt5
3
1
2 1
3_17
6
5
4_49
16
14
2
aj-e13
40
28
12
hwb4
11
10
1
served for function hwb8. Here, 35 gates are saved in total when output permutation is applied. But, not only the improvements are of interest. Even a comparison of the best and the worst permutation (shown in column d for ALL PERMS) give some interesting insight. For example, consider the function hwb5. One output permutation results in a circuit with 38 gates, while another permutation results in 62 gates. Since a heuristic synthesis procedure is used, the results will most likely not be optimal. In fact, according to Lemma 5.1 the best case improvement (i.e. the difference between the best and the worst permutation) for hwb5 cannot be greater than 3 · (5 − 1) = 12 for minimal realizations—yet it is 24. This can be explained by the heuristic nature of the approach that does not guarantee minimality. Finally it is shown that sifting provides good results in a fraction of the run-time. For most functions with more than six variables it is not feasible to minimize the circuit considering all permutations. However, sifting offers significant improvements for these cases.
5.3.4.4 Reductions Achieved by SWOP Finally, the quality (with respect to the number of gates) of some circuits synthesized by SWOP is compared to the currently best known realizations obtained by common synthesis approaches. Table 5.8 shows a selection of functions with the gate count of the currently best known circuit realization (denoted by B EST KNOWN). The obtained gate count when output permutation is considered is given in column SWOP. Synthesis with output permutation enables the realization of smaller circuits than the currently best known ones. As an interesting example, the realizations of the hwb4 function are observed in more detail. For the original function, a minimal realization with 11 gates has been synthesized by the exact approach. Now, using output permutation it is possible to synthesize a smaller realization with only 10 gates using a heuristic approach.
5.4 Summary and Future Work
111
5.4 Summary and Future Work Embedding irreversible functions is a new synthesis step, which is not needed in traditional circuit design. Nevertheless, it is a no less important aspect, since many practically relevant functions are irreversible and therewith need an embedding to become synthesizable in reversible logic. Usually, the embedding is done in a straightforward manner—Sect. 3.1.1 shows a possible approach. But, the way how the embedding is done is crucial to the resulting circuit sizes. This chapter gave examples showing this correlation. More precisely, two aspects have been investigated in detail. First, how to assign don’t cares was addressed. Don’t cares result if additional lines are added to make an irreversible function reversible. Second, the effect of different output permutations on the resulting circuits has been observed. Approaches have been proposed that exploit the respective possibilities so that significantly smaller circuits result. In doing so, it was not only possible to synthesize many important functions with smaller size; the described techniques (in particular the exact SWOP formulation from Sect. 5.3.2) also have been used to determine the respective BDD node substitutions for the BDD-based synthesis introduced in Sect. 3.2. In this sense, the made contributions are crucial in particular for synthesis of irreversible (sub-)functions that often occur in hardware design. In future work, the determination of “good” embeddings should be lifted from the truth table level to higher function representations. As an example, a 1-bit adder can efficiently be synthesized using the introduced techniques with minimal number of garbage lines, proper don’t care assignments, and the best output permutation. But, this cannot be ensured for a 32-bit adder with 232 = 4.3 · 109 truth table lines. Therefore, other synthesis techniques as e.g. the BDD-based synthesis approach from Sect. 3.2 must be applied. However, the embeddings generated by the BDDbased synthesis are not optimal with respect to the number of additional lines. Even simply cascading 32 optimally embedded 1-bit adders is not satisfying, since each 1-bit adder includes at least one constant input so that the final circuit would contain 32 additional lines. In fact, a 32-bit adder can be represented with at most one additional line only [CDKM05]. While for special functions like the adder “good” embeddings already have been manually found, developing new techniques that automatically generate embeddings for large functions are left for future work. Therewith, this chapter concludes the consideration of reversible logic synthesis in this book. In the next chapters the remaining aspects of an upcoming design flow, namely optimization as well as verification and debugging, are considered in detail.
Chapter 6
Optimization
The primary task of synthesis approaches is to generate circuits that realize the desired functions. Secondarily, it should be ensured that the resulting circuits are as compact as possible. However, the results obtained by synthesis approaches often are sub-optimal. For example, the transformation-based synthesis method described in Sect. 3.1.2 tends to produce circuits with very costly Toffoli gates (i.e. Toffoli gates with a large number of control lines). Hierarchical synthesis approaches like the BDD-based method from Sect. 3.2 or the SyReC-based approach from Sect. 3.3 lead to circuits that are not optimal with respect to the number of lines. Besides that, technology specific constraints are often not considered by synthesis approaches. Consequently, in common design flows optimization approaches are applied after synthesis. For reversible logic, only first attempts in optimization have been made in the last years. In particular, reducing the quantum cost of given circuits has been considered. For example, template matching [IKY02, MDM05, MYDM05] is a search method which looks for gate sequences that can be replaced by alternative cascades of lower cost. For many circuits, substantial improvements are achieved using this method. But, for large circuits or a high number of applied templates, respectively, this approach suffers from high run-time. As a second example, the work in [ZM06] showed how analyzing cross-point faults can identify redundant control connections in reversible circuits. Removing such control lines reduces the cost of the circuit. However, the computation needed to determine redundant control connections is extremely high. In this chapter, three new optimization approaches are introduced—each with an own focus on a particular cost metric. The first one considers the reduction of the well-established quantum cost (used in quantum circuits) and the transistor cost (used in CMOS implementations), respectively. Therefore, a (small) number of additional signal lines are added to the circuit that are used to “buffer” factors of control lines [MWD10]. Then, these factors can be reused by other gates in the circuit which reduces the size of the gates and thus decreases the cost of the circuit. A fast algorithm is presented along with results showing that even for a small number of additional lines (even 1) a significant amount of cost can be saved. R. Wille, R. Drechsler, Towards a Design Flow for Reversible Logic, DOI 10.1007/978-90-481-9579-4_6, © Springer Science+Business Media B.V. 2010
113
114
6 Optimization
The second approach consideres the line count in a circuit. While adding a small number of additional lines may be worth to reduce e.g. the quantum cost of a circuit, usually this number should be kept small (in particular for quantum circuits where circuit lines or qubits, respectively, are a limited resource). But as already mentioned above, in particular hierarchical approaches lead to a significant amount of additional circuit lines. To reduce these lines, a post-synthesis approach is introduced which re-synthesizes parts of the circuits so that lines with constant inputs can be merged [WSD10]. Therewith, notable line reductions can be achieved. Finally, an optimization method is introduced which takes a cost metric beyond the established quantum cost, transistor cost, and line count into account. This is motivated by new physical realizations of reversible and quantum circuits (see e.g. [DV02, Mas07, RO08]) leading to further limitations and restrictions. By means of so called Nearest Neighbor Cost (NNC), it is shown how reversible circuits can be optimized with respect to the resulting new cost metrics [WSD09]. NNC is important if Linear Nearest Neighbor (LNN) architectures [FDH04, Kut06, Mas07] are addressed as target technology. Here, only adjacent gates are allowed (i.e. gates where control line and target line are on adjacent circuit lines). Since ensuring adjacent gates in a naive way increases the quantum cost by about one order of magnitude, optimization approaches are introduced that significantly reduce this increase. At the end of this chapter, all results are summarized and future work is sketched.
6.1 Adding Lines to Reduce Circuit Cost This section shows how circuit cost can be significantly reduced, if additional lines are added to the circuit. Therefore, the general idea is firstly introduced in Sect. 6.1.1 before an algorithm exploiting the made observation is proposed in Sect. 6.1.2. Finally, the experimental results in Sect. 6.1.3 demonstrate the effect of the proposed optimization approach.
6.1.1 General Idea Optimization approaches, such as the two noted above preserved the number of lines in the circuit to be optimized. In contrast, this section shows how extending the circuit by additional signal lines can improve the cost of a reversible circuit. The additionally added lines are thereby denoted as helper lines in the following. Definition 6.1 Let G be a reversible circuit. Then, a helper line is an additionally added circuit line • whose input is set to a constant value 0 and • whose output is used as a garbage output.
6.1 Adding Lines to Reduce Circuit Cost
115
Fig. 6.1 Illustrating the general idea of factoring
Having a helper line available, values can be “buffered” on this line so that they can be later reused by other gates. In doing so, control lines can be saved as shown by the following definition. Definition 6.2 Let G be a reversible circuit and h be a helper line. Then, a gate ˆ t), MCT(C, t) of G can be replaced by the sequence MCT(F, h), MCT(h ∪ C, ˆ F ∩ Cˆ = ∅, and F = ∅. In the following this reMCT(F, h) where C = F ∪ C, placement is referred as factoring the initial gate, where F is a factor of MCT(C, t). Remark 6.1 The terminology “factoring” and “factor” are natural, since partitioning the control set C into F and Cˆ is essentially factoring the AND function for the control lines. This factoring depends on the fact that 0 ⊕ x1 x2 . . . xk = x1 x2 . . . xk , i.e. that the result of a factor can be “buffered” by a constant line assigned to 0. Applying Definition 6.2 to gates of a circuit, control lines can be removed. Since the number of control lines defines the amount of the circuit cost, this may lead to less costly circuits. However, this is only the case, if the total cost of the added gates is less than the cost saved by factorizing the control lines. By substituting a single gate only, this cannot happen for the transistor cost model but it can for the quantum cost model. If more than one gate can be substituted, higher cost savings are achieved (then also reductions for the transistor cost model are observed). These ideas are illustrated in the following example. Example 6.1 Consider the cascade of Toffoli gates depicted in Fig. 6.1(a). The gates in this cascade have a common control factor F = {x0 , x1 }. Hence, the cost of this circuit can be reduced as shown in Fig. 6.1(b) by adding an additional line h (at the top of the circuit) as well as the Toffoli gates MCT(F, h) before and after the cascade. This leads to additional quantum cost of 2 · 5 = 10. However, the factored gates reuse the result of F leading to a reduction of one control line per gate (dashed
116
6 Optimization
rectangle in Fig. 6.1(b)). The removed control lines are shown as white circles. In total this reduces the quantum cost from 104 to 59 and the transistor cost from 144 to 136, respectively. Note that the added line is set to constant input 0. Furthermore, the right most Toffoli gate operating on the added line is only needed if the line is to be used for another factor. Remark 6.2 In previous work, it has already been observed that more circuit lines usually lead to lower (quantum) cost (see e.g. [BBC+95] or also the results of the BDD-based approach discussed in Sect. 3.2.4). Moreover, the authors of [SPMH03] even showed that some functions cannot be synthesized for certain gate libraries unless one additional line is added. However, here these observations are exploited for the first time by proposing a constructive post-synthesis optimization approach for reversible logic. In the following section, the algorithm is now presented in detail.
6.1.2 Algorithm Based on the ideas presented in the last section, now an algorithm is proposed that adds one helper line and then employs a straightforward search procedure to use that line for optimizing the circuit. More precisely, it is shown how to extract factors from Toffoli and Fredkin gates in the circuit (the circuit may contain other types of gates). The algorithm can be applied repeatedly to add more than one helper line. It can also be iterated to add lines until adding a further line results in no cost reduction. The transistor cost model or the quantum cost model can be used and in fact the algorithm is readily adapted to any other gate-based cost model. Consider a reversible circuit G consisting of the cascade of gates G = g0 g2 . . . gd−1 . Let Ci denote the set of control lines for gi and let Ti denote the set of target lines for gi . Then, four steps are performed in total. 1. Add a single helper line h. 2. Find the highest cost reducing factor across the circuit. Therefore, the whole circuit is traversed (i.e. every gate gi with 0 ≤ i < d is considered). If gi is a reversible gate gi (Ci , Ti ) and the helper line h is available (i.e. it is not used by a previously applied factor at this point in the circuit), then ˆ with F not empty: for every partitioning of Ci into {F, C} a. Find the lowest j ≥ i so that j = d − 1 or (F ∩ (Tj +1 ∪ h)) = ∅, i.e. find the next gate gj that manipulates one of the factors in F so that the value of the helper line cannot be reused any longer. If the outputs of the circuit are reached use gd−1 instead. b. Determine the cost reduction that would result from applying this factor to all applicable gates between gi and gj , including the cost of introducing two instances of the factor gate MCT(F, h).
6.1 Adding Lines to Reduce Circuit Cost
117
c. Keep a record of the factor and the gate range that leads to the largest cost reduction. 3. If no cost reducing factor is found in Step 2, then terminate. 4. Otherwise, apply the best factor found and repeat from Step 2 on the revised circuit. Note that as already mentioned above, the rightmost MCT(F, h) gate operating on the helper line is only added if the helper line is going to be used for another factor. Example 6.2 Figure 6.2 shows the result of applying the algorithm to the circuit representing the function rd53 (depicted in Fig. 6.2(a)) using the quantum cost metric. The applied factors are highlighted by brackets at the bottom of Fig. 6.2(b) (with one helper line) and Fig. 6.2(c) (with two helper lines), respectively. While the original circuit has quantum cost of 128, that can be reduced with one helper line to 83 or with two helper lines to 66. Adding a third helper line does not reduce the quantum cost of this circuit further. The order in which factors are considered typically has an effect. As a result, the algorithm is applied to the circuit as given and then to the circuit found by reversing the order of the original circuit. The better of the two final circuits is taken as the result. Thus, the presented algorithm is a heuristic. But as the experiments in the next section show, already this leads to good results.
6.1.3 Experimental Results This section provides experimental results for the proposed approach. To this end, the method described above has been implemented in C and was applied to all benchmarks from RevLib [WGT+08]. All experiments have been carried out on an AMD Athlon 3500+ with 1 GB of memory. Since some of the circuits in RevLib already have been optimized using various approaches (e.g. extensive template post-synthesis optimization, output permutation optimization, and other techniques), to provide an even basis all circuits have been previously optimized. To this end, the approach described in [MDM05] together with a basic set of 14 templates has been used.1 Afterwards, the proposed optimization method has been applied to the resulting circuits. In doing so, all considered circuits already went through an optimization and it can be shown that, independently from this, further significant reductions can be achieved if helper lines and the algorithm introduced above are used.2 1 This took over 10 hours of computation time. Furthermore, the application to the urf series of circuits (which are quite large) has been aborted because they required too much run-time. 2 Of course, similar results are also achieved if the proposed approach is directly applied to nonoptimized circuits.
118
6 Optimization
Fig. 6.2 Reversible circuits for rd53 with one helper line and two helper lines
Table 6.1 summarizes the obtained results for one and two helper lines, respectively. The first three columns give the name of the circuit (including the unique identifier of the circuit realization as used in RevLib), the number of circuit lines (n), as well as the number of gates of the initial (already optimized) circuit (d), respectively. In the following columns, the obtained results for quantum cost and transistor cost models are presented. Therefore, the proposed approach has been applied with one and with two helper lines to both, the circuit as given as well as in the reversed order. Afterwards, the better result has been chosen. The re-
8
8
hwb8_117
hwb8_116
8
hwb8_113
9
9
hwb9_123
9
9
urf3_157
urf1_151
hwb9_120
10
urf1_150
hwb9_122
8
9
hwb8_114
8
10
9
urf5_159
urf3_156
8
hwb8_115
urf2_154
8
hwb8_118
16152
24523
14679
16510
17027
25843
38462
1202
6063
614
11941
48952
749
748
1535
1538
637
1959
1487
6976
7010
44635
44684
13460
22482
45855
2674 121716
1517
4338
4346
27402
27425
8251
13665
27740
72562
29120
7066
75644
9298
13979
8302
9333
9570
11804
16902
420
1877
69.04
37.82
38.00
38.61
38.62
38.70
39.22
39.50
40.38
40.51
40.83
40.98
42.43
43.00
43.44
43.47
43.80
54.32
56.06
65.06
3781
3776
20548
20574
6763
10996
20765
51833
21511
5869
53584
7164
8894
6478
7334
7324
9016
11533
290
1233
45.80
46.13
53.96
53.96
49.75
51.09
54.72
57.41
56.06
50.85
58.19
55.65
63.73
55.87
55.58
56.99
65.11
70.01
75.87
79.66
10536
10520
46336
46400
16896
28696
47024
100544
48616
14488
103680
17432
18640
15304
17648
18240
16200
21840
800
3272
10216
10192
41456
41496
15912
27760
41784
88928
43040
13640
91600
15768
15632
13912
15920
16352
13896
18576
568
2040
3.04
3.12
10.53
10.57
5.82
3.26
11.14
11.55
11.47
5.85
11.65
9.55
16.14
9.10
9.79
10.35
14.22
14.95
29.00
37.65
10144
10112
39544
39584
15648
27376
39760
81296
40824
13376
83160
15184
13704
13424
15384
15712
13336
17560
512
1768
3.72
3.88
14.66
14.69
7.39
4.60
15.45
19.14
16.03
7.68
19.79
12.90
26.48
12.28
12.83
13.86
17.68
19.60
36.00
45.97
2.31
2.28
8.62
8.68
3.19
6.30
8.73
19.64
9.03
2.83
20.16
3.16
3.16
2.83
3.20
3.28
3.30
4.19
0.17
1.63
M AX T RANSISTOR C OST M ODEL T IME I NITIAL A DD 1 L INE I NITIAL A DD 1 L INE A DD 2 L INES A DD 2 L INES C OST O PTIMIZED % I MPR . O PTIMIZED % I MPR . O PTIMIZED % I MPR . O PTIMIZED % I MPR . C OST C OST C OST C OST C OST
Q UANTUM C OST M ODEL
2732 128172
620
499
610
633
429
638
8
urf2_153
492
plus63mod8192_164 13
plus63mod4096_163 12
19
48
20
12
d
cycle10_2_110
n
cycle17_3_112
C IRCUIT
Table 6.1 Experimental results for RevLib circuits
6.1 Adding Lines to Reduce Circuit Cost 119
n
7
6
5
7
5
7
15
5
7
6
alu-v2_31
hwb7_61
4gt4-v0_78
rd53_136
ham15_108
4gt12-v0_87
rd53_135
hwb6_57
7
hwb7_62
hwb6_56
7
hwb7_59
ham7_104
5
4gt4-v0_73
5
7
hwb7_60
5
5
mod8-10_177
alu-v2_30
9
hwb9_121
4gt12-v0_86
9
hwb9_119
plus127mod8192_162 13
C IRCUIT
Table 6.1 (Continued)
d
910
65
16
10
70
15
13
236
13
126
23
18
14
331
289
17
166
14
1541
1544
433
68
43
403
72
53
3261
45
1329
83
111
47
2608
3939
57
1754
84
35973
35967
61425
326
51
32
294
52
38
2319
32
932
58
76
32
1775
2632
38
1153
55
23280
23269
38710
24.71
25.00
25.58
27.05
27.78
28.30
28.89
28.89
29.87
30.12
31.53
31.91
31.94
33.18
33.33
34.26
34.52
35.28
35.30
36.98
299
51
32
257
45
38
2105
32
871
58
62
32
1624
2310
38
1010
44
18940
18939
28856
30.95
25.00
25.58
36.23
37.50
28.30
35.45
28.89
34.46
30.12
44.14
31.91
37.73
41.36
33.33
42.42
47.62
47.35
47.34
53.02
976
224
104
992
200
144
5592
144
2456
272
240
136
4632
6800
144
3168
144
44304
44344
39984
928
216
104
968
200
112
5432
128
2392
272
208
104
4512
6480
144
2960
144
41408
41448
35304
4.92
3.57
0.00
2.42
0.00
22.22
2.86
11.11
2.61
0.00
13.33
23.53
2.59
4.71
0.00
6.57
0.00
6.54
6.53
11.70
928
216
104
968
200
112
5408
128
2392
272
176
104
4496
6400
144
2912
144
40336
40376
32648
4.92
3.57
0.00
2.42
0.00
22.22
3.29
11.11
2.61
0.00
26.67
23.53
2.94
5.88
0.00
8.08
0.00
8.96
8.95
18.35
0.28
0.05
0.05
0.22
0.05
0.05
1.04
0.06
0.51
0.07
0.08
0.05
1.01
1.33
0.07
0.82
0.05
8.65
8.70
8.36
M AX T RANSISTOR C OST M ODEL T IME I NITIAL A DD 1 L INE I NITIAL A DD 1 L INE A DD 2 L INES A DD 2 L INES C OST O PTIMIZED % I MPR . O PTIMIZED % I MPR . O PTIMIZED % I MPR . O PTIMIZED % I MPR . C OST C OST C OST C OST C OST
Q UANTUM C OST M ODEL
120 6 Optimization
15
7
ham15_107
sym6_145
36
132
28
16
5
7
5
hwb5_53
rd53_132
4gt13_91
10
27
55
109
3250
11
21
30
31
114
286
206
20465
65
121
230
53700
348
1174
106
65
27
99
247
176
17235
54
100
190
43893
279
916
81
49
5
10
hwb5_55
urf1_149
sym9_148
7
9 11554
rd53_133
24
cnt3-5_180
210
24
12
20
5
16
hwb5_54
72
1250
98
57770
73
120
63
1126
87
51125
64
105
9.92
11.22
11.50
12.33
12.50
12.50
12.55
12.90
13.16
13.64
14.56
15.78
16.92
17.36
17.39
18.26
19.83
21.98
23.58
24.62
1074
87
50424
64
105
63
113918
27
99
242
169
16594
54
100
174
42232
265
823
81
49
14.08
11.22
12.72
12.33
12.50
12.50
13.92
12.90
13.16
15.38
17.96
18.92
16.92
17.36
24.35
21.36
23.85
29.90
23.58
24.62
728
2360
224
176
96
176
824
1008
53664
200
216
344
232
320
240
3448
296
3432
296
184864 181816
240
320
240
423488 414016
128
176
824
1008
54416
200
216
344
171840 159120
744
2456
224
176
0.46
0.00
1.65
3.33
0.00
0.00
2.24
25.00
0.00
0.00
0.00
1.38
0.00
0.00
0.00
7.40
2.15
3.91
0.00
0.00
3416
296
181696
232
320
240
413656
96
176
824
1008
53664
200
216
344
157448
728
2336
224
176
0.93
0.00
1.71
3.33
0.00
0.00
2.32
25.00
0.00
0.00
0.00
1.38
0.00
0.00
0.00
8.38
2.15
4.89
0.00
0.00
1.11
0.07
51.47
0.05
0.09
0.09
182.19
0.05
0.06
0.18
0.27
12.03
0.04
0.06
0.08
227.23
0.18
0.67
0.07
0.05
M AX T RANSISTOR C OST M ODEL T IME I NITIAL A DD 1 L INE I NITIAL A DD 1 L INE A DD 2 L INES A DD 2 L INES C OST O PTIMIZED % I MPR . O PTIMIZED % I MPR . O PTIMIZED % I MPR . O PTIMIZED % I MPR . C OST C OST C OST C OST C OST
Q UANTUM C OST M ODEL
10 26468 132340 115731
8
15
5
one-two-three-v0_97
ham15_109
6
mod5adder_127
urf2_161
7
rd53_130
urf3_155
d
15 10740
7
urf6_160
7
rd53_131
n
rd53_137
C IRCUIT
Table 6.1 (Continued)
6.1 Adding Lines to Reduce Circuit Cost 121
32004
5
11
4gt5_76
urf4_187
5030
9
12
8
10
12
15
rd53_138
rd73_140
sym9_146
rd84_142
28
28
20
20
sys6-v0_111 10
13
10276
8
d
urf5_158
n
urf2_152
C IRCUIT
Table 6.1 (Continued) T RANSISTOR C OST M ODEL
111
108
75
43
71
160020
26
51380
25150
110
107
74
42
69
148333
24
46513
22693
0.90
0.93
1.33
2.33
2.82
7.30
7.69
9.47
9.77
110
107
74
42
68
146890
24
45662
22518
0.90
0.93
1.33
2.33
4.23
8.21
7.69
11.13
10.47
408
384
288
176
280
512064
112
164416
80480
400
376
280
168
264
501824
104
162496
79880
1.96
2.08
2.78
4.55
5.71
2.00
7.14
1.17
0.75
400
376
280
168
256
501280
104
162408
79840
1.96
2.08
2.78
4.55
8.57
2.11
7.14
1.22
0.80
0.04
44.47
18.30
M AX T IME
0.08
0.09
0.06
0.04
0.06
2135.51
I NITIAL A DD 1 L INE I NITIAL A DD 1 L INE A DD 2 L INES A DD 2 L INES C OST O PTIMIZED % I MPR . O PTIMIZED % I MPR . O PTIMIZED % I MPR . O PTIMIZED % I MPR . C OST C OST C OST C OST C OST
Q UANTUM C OST M ODEL
122 6 Optimization
6.1 Adding Lines to Reduce Circuit Cost
123
sulting cost and the percentage improvement are shown for each case relative to the initial circuit cost (i.e. the cost after template application). Finally, the last column gives the maximum CPU time (in seconds) measured for a single run for each benchmark. Results for small circuits with less than five lines and less than ten gates are omitted. Furthermore, the circuits 4gt11_82, 4gt13_90, decod24enable_126, mod5adder_128, mod5adder_129, hwb6_58, ham7_105, ham7_106, rd73_141, sys6-v0_144, sym9_147, 0410184_169, 0410184_170, rd84_143, cnt35_179, and add8_172 gave no improvement and thus are not listed in Table 6.1. Considering quantum cost, for most of the circuits significant cost reductions can be observed—even if only a single line is added. Over all circuits (including the ones that gave no improvement), adding a single line reduces the quantum cost by 22.51% on average—in the best case (cycle17_3_112) by just over 69%. This can be further improved if another line is added leading to reductions of additional 5.10% on average. If transistor cost is considered, the reductions are somewhat smaller but still significant. When adding a single line the transistor cost is reduced by 5.83% on average—in the best case (cycle17_3_112) by 37%. Adding a second line reduces the transistor cost by further 1.65%. Since the number of lines is negligible in CMOS technologies, this is a notable reduction as well. In addition, these optimizations can be achieved in very short run-time. Even for circuits including thousands of gates, the approach terminates after some minutes—in most of the cases after some seconds. Besides that, the effect of adding a certain number of helper lines on the resulting improvement has been evaluated in detail. More precisely, the proposed method has been applied with one to five helper lines to all the circuits from RevLib (including the small ones that have been omitted in Table 6.1). Again, all these circuits already have been optimized using templates as noted above. A total of 95 of the 177 circuits show an improvement in quantum cost when a single helper line is added. Of the other 82 circuits, 64 ones have a very few number of lines (less than or equal to 5) and are already highly optimized due to their relatively small size. Figure 6.3 shows the improvement in quantum cost of the remaining circuits (both, for the respective benchmarks in the plot diagram as well as on average in the table). As already discussed above, a significant improvement can be observed if a single helper line is added. This is further increased if more lines are applied. However, the improvements diminish with increasing number of helper lines. Finally, no further improvement has been observed, if a sixth line is applied. This is the expected behavior, since multiple helper lines are only useful when multiple factors sharing common gates are present. Altogether, applying the proposed approach significant cost reductions can be achieved if a single line is added to the circuit (even on already optimized realizations). Further (diminishing) improvements result if more than one helper lines are applied. The most critical issue is the fact that additional lines must be added to enable these optimizations. While this is negligible for reversible CMOS technologies, in design for quantum circuits the designer must trade off if these additional expenses are worth the additional qubit(s). Since up to 70% of the quantum cost can be saved, this may be the case for many circuits.
124
6 Optimization
Fig. 6.3 Improvement for up to five helper lines
6.2 Reducing the Number of Circuit Lines While adding a small number of additional lines may be worth to reduce e.g. the quantum cost of a circuit (as shown in the previous section), usually circuit lines are a highly limited resource (caused by the fact that the number of circuit lines corresponds to the number of qubits). Furthermore, a high number of lines (or qubits, respectively) may decrease the reliability of the resulting system. Thus, this number should be kept as small as possible. In the best case, only the minimal number of circuit lines should be used. However, to ensure minimality of circuit lines, the underyling function must be given in terms of a truth table or similar descriptions (see Sect. 3.1.1). But, if larger functions should be synthesized, only hierarchical methods (like the BDD-based method from Sect. 3.2 or the SyReC-based approach from Sect. 3.3) are available so far. These often require a significant number of additional circuit lines (with constant inputs) and, thus, lead to circuits with a large line count. As an example, consider the reversible realization of the AND function and the OR function as shown in Fig. 6.4(a) and (b), respectively. Composing these circuits (as done by hierarchical approaches), a realization with two additional circuit lines (including constant inputs) results (see Fig. 6.4(c)). But, both functions combined can be realized with one additional circuit line only (see Fig. 6.4(d)). Thus, the question is how the number of additional lines in reversible circuits can be reduced. In this section, a post-process optimization method is proposed that addresses this problem. Garbage outputs (i.e. circuit lines whose output value is don’t care) are thereby exploited. A multi-stage approach is introduced that (1) identifies garbage outputs producing don’t cares, (2) re-synthesizes parts of the circuit so that instead of these don’t cares concrete constant values are computed, and (3) connects the resulting outputs with appropriate constant inputs. In other words, circuit structures are modified so that they can be merged with constant inputs resulting in a line reduction. For the respective re-synthesis step, existing synthesis methods are used.
6.2 Reducing the Number of Circuit Lines
125
Fig. 6.4 Composition of circuits
Experimental results show that applying this approach, the number of circuit lines can be reduced by 17% on average—in the best case by more than 40%. Furthermore, depending on the used synthesis approach, these line reductions are possible only with a small increase in the number of gates and the quantum costs, respectively. In some cases the costs even can be reduced. In this sense, drawbacks of scalable but line-costly synthesis approaches are minimized. The remainder of this section is structured as follows. Section 6.2.1 illustrates the general idea of the proposed approach. Afterwards, the concrete algorithm exploiting the made observations is described in Sect. 6.2.2. Finally, Sect. 6.2.3 reports experimental results.
6.2.1 General Idea In this section, the idea how to reduce the number of lines in large reversible circuits is presented. As discussed above, ensuring minimality of circuit lines is only possible for small functions from which a truth table description can be derived. Thus, line reduction is considered as a post-optimization problem. The proposed approach thereby exploits a structure often occurring in circuits generated by scalable synthesis approaches or by composed reversible sub-circuits. This is illustrated by the following running example. Example 6.3 Consider the circuit G = g1 . . . g12 depicted in Fig. 6.5(a) representing a 3-bit adder that has been created by composing three single (minimal) 1-bit adders. This circuit consists of three additional circuit lines (with constant inputs). Not all of them are necessarily required. Furthermore, there are a couple of garbage outputs whose values are don’t care. In particular of interest in this circuit is the first usage of a line with a constant input and the last usage of a line with a garbage output. For example, the constant
126
6 Optimization
Fig. 6.5 Reducing the number of lines in a 3-bit adder circuit
input at line 4 is firstly used by the fifth gate, while at the same time the value of the last line is not needed anymore after the second gate. Since the value of the garbage output doesn’t matter (because it is a don’t care), this might offer the possibility to merge the line including the constant input with the line including the garbage output. More precisely, if it is possible to modify the circuit so that a garbage output returns a constant value (instead of an arbitrary value), then this constant value can be used in the rest of this circuit. At the same time, a constant input line can be removed. More formally: Proposition 6.1 Without loss of generality, let G = g1 . . . gd be a reversible circuit with a constant input at line lc and a garbage output at line lg (lc = lg ). Furthermore, let gi be the first gate connected with line lc (including the constant input) and let gj with j < i be the last gate connected with line lg (including the garbage output). If it j is possible to modify the sub-circuit G1 = g1 . . . gj so that line lg becomes assigned to a constant value, then line lc can be removed from G. For all gates formerly connected with line lc , line lg can be used instead.
6.2 Reducing the Number of Circuit Lines
127
Note that the constant value of the selected line lc is thereby of no importance. If necessary, the needed value can easily be generated by an additional NOT gate (i.e. a Toffoli gate without any control lines). Furthermore, constant outputs can only be produced if the considered circuit includes additional lines with constant inputs. Example 6.4 Reconsider the adder circuit G = g1 . . . g12 in the running example. The constant input at line 1 is firstly used by gate g9 , while the values of the garbage outputs at line 5, line 6, line 9, and line 10, respectively, are not needed anymore after gate g8 . Since the sub-circuit G81 = g1 . . . g8 can be modified so that e.g. the garbage output at line 5 becomes assigned to the constant value 0 (see dashed rectangle in Fig. 6.5(b)), line 1 can be removed and the newly created constant value from line 5 can be used instead. The resulting circuit is depicted in Fig. 6.5(b). Now, this circuit consists of 9 instead of 10 lines. Note that the respective modification of a sub-circuit is not always possible. For example, consider the constant input at line 4 (firstly used by gate g5 ) and the garbage outputs at line 9 and line 10 (not needed anymore after gate g4 ). This might offer the possibility to remove one more circuit line. But, the sub-circuit G41 = g1 . . . g4 cannot be modified accordingly since a realization of the 1-bit addition together with an additional constant output requires more garbage outputs. Using these observations an algorithm for reducing the number of lines in reversible circuits can be formulated. The next section describes the respective steps in detail. Afterwards, the experiments in Sect. 6.2.3 show that significant reductions can be obtained with this approach.
6.2.2 Algorithm Based on the ideas presented in the last section, now an algorithm for circuit line reduction is proposed. The respective steps are illustrated by means of an example in Fig. 6.6. At first, an appropriate sub-circuit is determined (a). Afterwards, it is tried to re-synthesize the sub-circuit so that one of the (garbage) outputs returns a constant value (b). If this was successful, the re-synthesized sub-circuit is inserted in