Statistics
5
Views
0
Downloads
0
Donations
Support
Share
Uploader

高宏飞

Shared on 2026-05-30
Support Statistics
¥.00 · 0times
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

(This page has no text content)
Qiskit Pocket Guide The quantum computing market is predicted to grow by nearly $1.3 billion over the next five years. Why? Given their quantum mechanical nature, quantum computers are expected to solve difficult problems in chemistry, optimization, finance, and machine learning that classical computers find impossible to unravel. This pocket guide provides software developers with a quick reference to Qiskit, an open source SDK for working with quantum computers. Packed with helpful programming examples, tables, figures, and lists, this handy book helps you find the information you need to develop and debug quantum programs. Whether you’re focused on business, engineering, or scientific applications, you’ll learn how to choose and apply appropriate Qiskit classes, methods, and functions. • Learn how to create quantum circuits, define quantum gates, and leverage the transpiler • Explore modules for implementing quantum information concepts and quantum algorithms • Survey features of Qiskit that abstract and facilitate working with various quantum computers and simulators • Learn how to use the latest version of the Open Quantum Assembly Language QUANTUM COMPUTING US $24.99 CAN $31.99 ISBN: 978-1-098-11247-9 Twitter: @oreillymedia linkedin.com/company/oreilly-media youtube.com/oreillymedia
Praise for Qiskit Pocket Guide Qiskit Pocket Guide is a convenient, compact Qiskit reference book for quantum programming all in one place. —Jay Gambetta, IBM Fellow and VP of IBM Quantum This pocket guide is an excellent overview of core Qiskit functionality with a concise, yet practical, code-first approach. —Abby Mitchell, Quantum Developer Advocate Learning quantum computing can be daunting, and remembering all the functionality in the Qiskit API can be equally so. This book provides a necessary companion for coders who want to take full advantage of the power and elegant features provided by Qiskit. —Dr. Bob Sutor, Quantum industry expert and author, Dancing with Qubits and Dancing with Python
This is an incredibly handy book–for beginners and advanced users alike–that describes the many functionalities and features of Qiskit, organized in a clear and intuitive way. I expect it is unlikely to find its way off my desk and into a bookshelf any time soon. —Dr. John Watrous, Technical Director, IBM Quantum Education Concise yet extensive, this belongs in the pocket of every Qiskit developer. —Lia Yeh, CS PhD student, Quantum Group, University of Oxford Very crisp and clear book for quantum developers of all levels! —Robert Loredo, IBM Quantum Ambassador Worldwide Lead A clear and well-organized reference; readers exploring quantum programming will enjoy simple and specific steps to get QPU programs up and running, while even experienced Qiskit users may discover some surprising features. —Eric R. Johnston, coauthor of Programming Quantum Computers
James L. Weaver and Frank J. Harkins Qiskit Pocket Guide Quantum Development with Qiskit
978-1-098-11247-9 [LSI] Qiskit Pocket Guide by James L. Weaver and Frank J. Harkins Copyright © 2022 James Weaver and Francis Harkins. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebasto‐ pol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promo‐ tional use. Online editions are also available for most titles (http://oreilly.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com. Acquisitions Editor: Suzanne McQuade Development Editor: Shira Evans Production Editor: Katherine Tozer Copyeditor: Piper Editorial Consulting, LLC Proofreader: Tom Sullivan Indexer: nSight, Inc. Interior Designer: David Futato Cover Designer: Karen Montgomery Illustrator: Kate Dullea July 2022: First Edition Revision History for the First Release 2022-06-15: First Release See https://oreil.ly/qkpgERR for release details. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Qiskit Pocket Guide, the cover image, and related trade dress are trademarks of O’Reilly Media, Inc. The views expressed in this work are those of the authors and do not repre‐ sent the publisher’s views. While the publisher and the authors have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the authors disclaim all respon‐ sibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights.
Table of Contents Preface ix Part I. Qiskit Fundamentals Chapter 1: Quantum Circuits and Operations 3 Constructing Quantum Circuits 3 Instructions and Gates 26 Parameterized Quantum Circuits 32 Chapter 2: Running Quantum Circuits 35 Using the BasicAer Simulators 36 Using the Aer Simulators 41 Monitoring Job Status and Obtaining Results 51 Chapter 3: Visualizing Quantum Measurements and States 55 Visualizing Measurement Counts 55 Visualizing Quantum States 57 v
Chapter 4: Using the Transpiler 67 Quickstart with Transpile 67 Transpiler Passes 72 Part II. Quantum Information and Algorithms Chapter 5: Quantum Information 85 Using Quantum Information States 85 Using Quantum Information Operators 95 Using Quantum Information Channels 101 Using Quantum Information Measures 103 Chapter 6: Operator Flow 105 Creating Operator Flow Expressions 105 Using the Operator Flow State Function Classes 109 Using the Operator Flow Primitive Operators Classes 113 Chapter 7: Quantum Algorithms 119 Background on Quantum Algorithms 119 Using the Algorithms Module 121 Traditional Quantum Algorithms 124 Eigensolvers 133 Part III. Additional Essential Functionality Chapter 8: Qiskit Circuit Library Standard Operations 151 Standard Instructions 151 Standard Single-Qubit Gates 152 Standard Multiqubit Gates 157 vi | Table of Contents
Chapter 9: Working with Providers and Backends 165 Graphical Tools 166 Text-Based Tools 168 Getting System Info Programmatically 170 Interacting with Quantum Systems on the Cloud 172 Chapter 10: OpenQASM 175 Building Quantum Circuits in QASM 175 Building Higher-Level Gates 182 Classical Types and Instructions 185 Building Quantum Programs 192 Index 195 Table of Contents | vii
(This page has no text content)
Preface Qiskit is an open source SDK (software development kit) for working with quantum computers at the level of pulses, cir‐ cuits, and application modules. The purpose of this book is to provide a succinct guide for developers to use while creating applications that leverage quantum computers and simulators. We hope this book will enable developers to actively participate in the open source Qiskit community. The Qiskit community, as well as documentation, tutorials, and other resources, is available at Qiskit. How This Book Is Structured Our goal in this guide is to address much of the functionality of Qiskit that application developers will routinely use. Some of this Qiskit functionality is considered to be fundamental to quantum computing. Other Qiskit functionality supports quantum computing concepts such as quantum information and quantum algorithms. Qiskit has additional functionality that we’ve deemed essential for quantum application develop‐ ment. We’ve structured the book at a high level according to the aforementioned functionality, with individual chapters drilling into the specifics. The chapters are divided into three parts: ix
Part I, Qiskit Fundamentals In the first part of the book, we show you how to use Qis‐ kit to create quantum circuits. Quantum circuits contain instructions and gates, so we discuss how to use the ones provided in Qiskit, as well as how to create your own. We then show you how to run quantum circuits on quantum computers and simulators and demonstrate how to visual‐ ize results. To round out Part I, we discuss the transpiler and how it converts a quantum circuit into instructions that run on a target quantum computer or simulator. Part II, Quantum Information and Algorithms In the second part of this book, we discuss Qiskit modules responsible for implementing quantum information con‐ cepts (specifically states, operators, channels, and meas‐ ures). We also present facilities in Qiskit that implement quantum algorithms, as well as a facility known in Qiskit as operator flow. A developer may use some of the func‐ tionality in Part II to develop quantum applications at higher levels of abstraction than quantum circuits. Part III, Additional Essential Functionality In the third and final part of this book, we cover essential Qiskit functionality, some of which drills into information already discussed and some of which is newly presented. Specifically, we explore the standard operations of the Qis‐ kit circuit library, and new ground is uncovered when we discuss how to work with quantum providers and back‐ ends. In addition, we’ll introduce QASM 3.0 and demon‐ strate how to create quantum programs with this quantum assembly language. Conventions Used in This Book The following typographical conventions are used in this book: Italic Indicates new terms, URLs, email addresses, filenames, and file extensions. x | Preface
Constant width Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, state‐ ments, and keywords. NOTE This element signifies a general note. Using Code Examples Supplemental material (code examples, exercises, etc.) is available for download at https://github.com/qiskit-community/ qiskit-pocket-guide#readme. If you have a technical question or a problem using the code examples, please send email to bookquestions@oreilly.com. This book is here to help you get your job done. In general, if example code is offered with this book, you may use it in your programs and documentation. You do not need to con‐ tact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permis‐ sion. Selling or distributing examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incor‐ porating a significant amount of example code from this book into your product’s documentation does require permission. We appreciate, but generally do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “Qiskit Pocket Guide by James L. Weaver and Frank J. Harkins (O’Reilly). Copyright 2022 James Weaver and Frank Harkins, 978-1-098-11247-9.” Preface | xi
If you feel that your use of code examples falls outside fair use or the permission given above, feel free to contact us at permissions@oreilly.com. O’Reilly Online Learning For more than 40 years, O’Reilly Media has provided technology and business training, knowledge, and insight to help companies succeed. Our unique network of experts and innovators share their knowledge and expertise through books, articles, and our online learning platform. O’Reilly’s online learning platform gives you on-demand access to live training courses, in-depth learning paths, interactive coding environments, and a vast collection of text and video from O’Reilly and 200+ other pub‐ lishers. For more information, visit http://oreilly.com. How to Contact Us Please address comments and questions concerning this book to the publisher: O’Reilly Media, Inc. 1005 Gravenstein Highway North Sebastopol, CA 95472 800-998-9938 (in the United States or Canada) 707-829-0515 (international or local) 707-829-0104 (fax) We have a web page for this book, where we list errata, exam‐ ples, and any additional information. You can access this page at https://oreil.ly/qiskit-pocket-guide. Email bookquestions@oreilly.com to comment or ask technical questions about this book. For news and information about our books and courses, visit https://oreilly.com. xii | Preface
Find us on LinkedIn: https://linkedin.com/company/oreilly- media. Follow us on Twitter: https://twitter.com/oreillymedia. Watch us on YouTube: https://www.youtube.com/oreillymedia. Acknowledgments This book would not have been possible without a support‐ ing team of innovative people at IBM Quantum and in the larger quantum computing community. The authors would like to thank the amazing O’Reilly team, including Kristen Brown, Danny Elfanbaum, Shira Evans, Zan McQuade, Jona‐ thon Owen, Kim Sandoval, and Katherine Tozer. The authors also appreciate the invaluable contributions made by techni‐ cal reviewers Luciano Bello, Nick Bronn, Barry Burd, Junye Huang, Eric Johnston, Robert Loredo, and Iskandar Sitdikov. James Weaver would like to thank Julie, Lori, Kelli, Kaleb, Jillian, Levi, and Oliver for their understanding and encour‐ agement while working on this book. As a lifelong classical developer, James is also thankful that quantum mechanical phenomena baked into nature may potentially be leveraged to solve problems not possible with classical computers. “The heavens declare the glory of God, and the sky above proclaims his handiwork” (Psalm 19:1). Frank Harkins would like to thank the Qiskit team for all their great work on Qiskit and its documentation and for answering all his questions. Frank would also like to thank Rose, Matt, Joanne, Keith, Libby, and Martha for their constant support over the course of writing this book. Preface | xiii
(This page has no text content)
PART I Qiskit Fundamentals If one has left this entire system to itself for an hour, one would say that the cat still lives if meanwhile no atom has decayed. The psi-function of the entire system would express this by having in it the living and dead cat (par‐ don the expression) mixed or smeared out in equal parts. —Erwin Schrödinger Underlying all programs developed using Qiskit are some fun‐ damental concepts and modules. In the first part of this book, we’ll explore these fundamentals, beginning with Chapter 1, “Quantum Circuits and Operations”. In that chapter, we’ll demonstrate how to create quantum circuits, populate them with commonly used gates and instructions, obtain informa‐ tion about quantum circuits, and manipulate them. In Chapter 2, “Running Quantum Circuits”, we’ll demonstrate how to use Qiskit classes and functions to run quantum cir‐ cuits on quantum simulators and devices. We’ll also show you how to monitor the status of a job, as well as how to obtain
its results. Then in Chapter 3, “Visualizing Quantum Measure‐ ments and States”, we’ll show you how to leverage graphical features of Qiskit to visualize quantum states and results. Finally in Chapter 4, “Using the Transpiler”, we’ll discuss the process of transpilation in which the operations of a quantum circuit are converted into instructions for running on a particu‐ lar quantum simulator or device.
CHAPTER 1 Quantum Circuits and Operations In Qiskit, quantum programs are normally expressed with quantum circuits that contain quantum operations. Quantum circuits are represented by the QuantumCircuit class, and quan‐ tum operations are represented by subclasses of the class Instruction. Constructing Quantum Circuits A quantum circuit may be created by supplying an argument that indicates the number of desired quantum wires (qubits) for that circuit. This is often supplied as an integer: from qiskit import QuantumCircuit QuantumCircuit(2) Optionally, the number of desired classical wires (bits) may also be specified. The first argument refers to the number of quan‐ tum wires, and the second argument the number of classical wires: QuantumCircuit(2, 2) The number of desired quantum and classical wires may also be expressed by supplying instances of QuantumRegister and ClassicalRegister as arguments to QuantumCircuit. These 3
classes are addressed in “Using the QuantumRegister Class” on page 24 and “Using the ClassicalRegister Class” on page 25. Using the QuantumCircuit Class The QuantumCircuit class contains a large number of methods and attributes. The purpose of many of its methods is to apply quantum operations to a quantum circuit. Most of its other methods and attributes either manipulate or report informa‐ tion about a quantum circuit. Commonly used gates Table 1-1 contains some commonly used single-qubit gates and code examples. The variable qc refers to an instance of QuantumCircuit that contains at least four quantum wires. Table 1-1. Commonly used single-qubit gates in Qiskit Names Example Notes H, Hadamard qc.h(0) Applies H gate to qubit 0. See “HGate” on page 153. I, Identity qc.id(2) or qc.i(2) Applies I gate to qubit 2. See “IGate” on page 153. P, Phase qc.p(math.pi/ 2,0) Applies P gate with π/2 phase rotation to qubit 0. See “PhaseGate” on page 153. RX qc.rx(math.pi/ 4,2) Applies RX gate with π/4 rotation to qubit 2. See “RXGate” on page 153. RY qc.ry(math.pi/ 8,0) Applies RY gate with π/8 rotation to qubit 0. See “RYGate” on page 154. RZ qc.rz(math.pi/ 2,1) Applies RZ gate with π/2 rotation to qubit 1. See “RZGate” on page 154. S qc.s(3) Applies S gate to qubit 3. Equivalent to P gate with π/2 phase rotation. See “SGate” on page 154. 4 | Chapter 1: Quantum Circuits and Operations