Flutter插件qartvm的使用_Dart和Flutter的量子计算模拟包

Flutter插件qartvm的使用_Dart和Flutter的量子计算模拟包

qartvm(发音为’kar-toom’,就像苏丹的首都)是一个用于Dart和Flutter的量子计算模拟包。

特性

  • 量子电路定义
  • 内置量子门:
    • Hadamard
    • Pauli X (NOT),Y,Z
    • Phase S,T及自定义门
    • 并行单量子比特门
    • 控制门(单量子比特控制)
    • 高级门:
      • swap
      • Toffoli (CC-NOT)
      • Fredkin (C-SWAP)
      • 量子傅里叶变换(QFT)及其逆变换
  • 自定义量子门
  • 量子寄存器(n量子比特)

示例

一些示例可以在/example文件夹中找到。

Bell State

final circuit = QCircuit(size: 2);
circuit.hadamard(0);
circuit.controlledNot(0, 1);

describe(circuit);
draw(circuit);

final qreg = QRegister.zero(2);
print('Initial states');
print(' * amplitudes:    ${amplInfo(qreg.amplitudes, fractionDigits: 6)}');
print(' * probabilities: ${probInfo(qreg.probabilities, fractionDigits: 2)}');
circuit.execute(qreg);
print('Final states');
print(' * amplitudes:    ${amplInfo(qreg.amplitudes, fractionDigits: 6)}');
print(' * probabilities: ${probInfo(qreg.probabilities, fractionDigits: 2)}');

输出:

 * hadamard on [0]
 * pauliX on [1] controlled by [0]
          ---
   0 ----| H |---- X ------
          ---
                 -----
   1 -----------| NOT |----
                 -----
Initial states
 * amplitudes:    00 (1.000000)
 * probabilities: 00 (100.00 %)
Final states
 * amplitudes:    00 (0.707107), 11 (0.707107)
 * probabilities: 00 (50.00 %), 11 (50.00 %)

Qubit Full Adder

final circuit = QCircuit(size: 4);
circuit.toffoli(0, 1, 3);
circuit.controlledNot(0, 1);
circuit.toffoli(1, 2, 3);
circuit.controlledNot(1, 2);
circuit.controlledNot(0, 1);

describe(circuit);
draw(circuit);  

for (var a = 0; a <= 1; a++) {
  for (var b = 0; b <= 1; b++) {
    final qreg = QRegister([
      if (a == 1) Qbit.one else Qbit.zero,
      if (b == 1) Qbit.one else Qbit.zero,
      Qbit.zero,
      Qbit.zero
    ]);

    print('[$a/$b] Initial: ${stateInfo(qreg.probabilities)}');

    circuit.execute(qreg);

    print('[$a/$b] Outcome: ${stateInfo(qreg.probabilities)}');

    final result = qreg.read(qubits: [3, 2]);

    print('[$a/$b] $a + $b = $result');
  }
}

输出:

 * toffoli on [3] controlled by [0, 1]
 * pauliX on [1] controlled by [0]
 * toffoli on [3] controlled by [1, 2]
 * pauliX on [2] controlled by [1]
 * pauliX on [1] controlled by [0]

   0 ======= X ======== X =========================== X ======

                      -----                         -----
   1 ======= X ======| NOT |===== X ======== X ====| NOT |====
                      -----                         -----
                                           -----
   2 ============================ X ======| NOT |=============
                                           -----
          --------             --------
   3 ====| CC-NOT |===========| CC-NOT |======================
          --------             --------
[0/0] Initial: 0000 (100 %)
[0/0] Outcome: 0000 (100 %)
[0/0] 0 + 0 = 0
[0/1] Initial: 0100 (100 %)
[0/1] Outcome: 0110 (100 %)
[0/1] 0 + 1 = 1
[1/0] Initial: 1000 (100 %)
[1/0] Outcome: 1010 (100 %)
[1/0] 1 + 0 = 1
[1/1] Initial: 1100 (100 %)
[1/1] Outcome: 1101 (100 %)
[1/1] 1 + 1 = 2

Addition of 2 2-qubit Registers

// qubit #   =  input bit /  result bit
//    0      =      a0    /    (a+b)0
//    1      =      a1    /    (a+b)1
//    2      =      a2    /    (a+b)2 (carry)
//    3      =      b0    /      b0
//    4      =      b1    /      b1
//    5      =      |0>                           (suppressed as this qubit is useless)
final circuit = QCircuit(size: 5);

circuit.qft([2, 1, 0]);

// circuit.controlledPhase(5, 2, math.pi); // suppressed because qubit 5 is always |0>
circuit.controlledPhase(4, 2, math.pi / 2);
circuit.controlledPhase(3, 2, math.pi / 4);

circuit.controlledPhase(4, 1, math.pi);
circuit.controlledPhase(3, 1, math.pi / 2);

circuit.controlledPhase(3, 0, math.pi);

circuit.invQft([2, 1, 0]);

describe(circuit);
draw(circuit);

final sw = Stopwatch();

sw.start();
verifyAddition(circuit);
sw.stop();
print('Completed in ${sw.elapsed} before compilation, total executions = $_nbExec (${sw.elapsedMicroseconds.toDouble() / _nbExec} µs/execution)');

circuit.compile();

describe(circuit);
draw(circuit);

sw.reset();
sw.start();
verifyAddition(circuit);
sw.stop();
print('Completed in ${sw.elapsed} after compilation, total executions = $_nbExec (${sw.elapsedMicroseconds.toDouble() / _nbExec} µs/execution)');

输出:

 * qft on [2, 1, 0]
 * phase 0.5 pi on [2] controlled by [4]
 * phase 0.25 pi on [2] controlled by [3]
 * phase 1.0 pi on [1] controlled by [4]
 * phase 0.5 pi on [1] controlled by [3]
 * phase 1.0 pi on [0] controlled by [3]
 * invqft on [2, 1, 0]
          -----                                                                 -----------    ---------     
   0 ----| QFT |---------------------------------------------------------------| P(1.0 pi) |--| INV-QFT |----
         |     |                                                                -----------   |         |    
         |     |                                  -----------    -----------                  |         |    
   1 ----| QFT |---------------------------------| P(1.0 pi) |--| P(0.5 pi) |-----------------| INV-QFT |----
         |     |                                  -----------    -----------                  |         |    
         |     |   -----------    ------------                                                |         |    
   2 ----| QFT |--| P(0.5 pi) |--| P(0.25 pi) |-----------------------------------------------| INV-QFT |----
          -----    -----------    ------------                                                 ---------

   3 --------------------------------- X ---------------------------- X ------------ X ----------------------

   4 ------------------ X ---------------------------- X ----------------------------------------------------

[0/0] Outcome: 0 + 0 = {0: 100}
[0/1] Outcome: 0 + 1 = {1: 100}
[0/2] Outcome: 0 + 2 = {2: 100}
[0/3] Outcome: 0 + 3 = {3: 100}
[1/0] Outcome: 1 + 0 = {1: 100}
[1/1] Outcome: 1 + 1 = {2: 100}
[1/2] Outcome: 1 + 2 = {3: 100}
[1/3] Outcome: 1 + 3 = {4: 100}
[2/0] Outcome: 2 + 0 = {2: 100}
[2/1] Outcome: 2 + 1 = {3: 100}
[2/2] Outcome: 2 + 2 = {4: 100}
[2/3] Outcome: 2 + 3 = {5: 100}
[3/0] Outcome: 3 + 0 = {3: 100}
[3/1] Outcome: 3 + 1 = {4: 100}
[3/2] Outcome: 3 + 2 = {5: 100}
[3/3] Outcome: 3 + 3 = {6: 100}
[4/0] Outcome: 4 + 0 = {4: 100}
[4/1] Outcome: 4 + 1 = {5: 100}
[4/2] Outcome: 4 + 2 = {6: 100}
[4/3] Outcome: 4 + 3 = {7: 100}
Completed in 0:00:01.382940 before compilation, total executions = 12000 (115.245 µs/execution)
 * qft on [2, 1, 0] followed by phase 0.5 pi on [2] controlled by [4] followed by phase 0.25 pi on [2] controlled by [3] followed by phase 1.0 pi on [1] controlled by [4] followed by phase 0.5 pi on [1] controlled by [3] followed by phase 1.0 pi on [0] controlled by [3] followed by invqft on [2, 1, 0]
          ----------
   0 ----| COMPILED |----
         |          |
         |          |
   1 ----| COMPILED |----
         |          |
         |          |
   2 ----| COMPILED |----
          ----------

   3 -------- X ---------

   4 -------- X ---------

[0/0] Outcome: 0 + 0 = {0: 100}
[0/1] Outcome: 0 + 1 = {1: 100}
[0/2] Outcome: 0 + 2 = {2: 100}
[0/3] Outcome: 0 + 3 = {3: 100}
[1/0] Outcome: 1 + 0 = {1: 100}
[1/1] Outcome: 1 + 1 = {2: 100}
[1/2] Outcome: 1 + 2 = {3: 100}
[1/3] Outcome: 1 + 3 = {4: 100}
[2/0] Outcome: 2 + 0 = {2: 100}
[2/1] Outcome: 2 + 1 = {3: 100}
[2/2] Outcome: 2 + 2 = {4: 100}
[2/3] Outcome: 2 + 3 = {5: 100}
[3/0] Outcome: 3 + 0 = {3: 100}
[3/1] Outcome: 3 + 1 = {4: 100}
[3/2] Outcome: 3 + 2 = {5: 100}
[3/3] Outcome: 3 + 3 = {6: 100}
[4/0] Outcome: 4 + 0 = {4: 100}
[4/1] Outcome: 4 + 1 = {5: 100}
[4/2] Outcome: 4 + 2 = {6: 100}
[4/3] Outcome: 4 + 3 = {7: 100}
Completed in 0:00:00.425942 after compilation, total executions = 12000 (35.49516666666667 µs/execution)

Qubit Teleportation

final circuit = QCircuit(size: 3);
circuit.hadamard(1);
circuit.controlledNot(1, 2);
circuit.controlledNot(0, 1);
circuit.hadamard(0);
circuit.measure(qubits: {0});
circuit.measure(qubits: {1});
circuit.controlledNot(1, 2);
circuit.controlledPauliZ(0, 2);

print('');
print('Verification before compilation');
describe(circuit);
draw(circuit);
checkTeleportation(circuit);

circuit.compile();

print('');
print('Verification after compilation');
describe(circuit);
draw(circuit);
checkTeleportation(circuit);

输出:

Verification before compilation
 * hadamard on [1]
 * pauliX on [2] controlled by [1]
 * pauliX on [1] controlled by [0]
 * hadamard on [0]
 * measure [0]
 * measure [1]
 * pauliX on [2] controlled by [1]
 * pauliZ on [2] controlled by [0]
                                   ---
   0 ---------------------- X ----| H |---[ / ]---------------------- X -----
                                   ---
          ---             -----
   1 ----| H |---- X ----| NOT |-------------------[ / ]----- X -------------
          ---             -----
                 -----                                      -----    ---
   2 -----------| NOT |------------------------------------| NOT |--| Z |----
                 -----                                      -----    ---
Initial states: 000 (0.467916 + 0.773411 i), 100 (-0.427065 + 0.022487 i)
   Alice: 0 (81.71 %) / 1 (18.29 %)
Final states: 100 (0.467916 + 0.773411 i), 101 (-0.427065 + 0.022487 i)
   Bob  : 0 (81.71 %) / 1 (18.29 %)

Verification after compilation
 * hadamard on [1] followed by pauliX on [2] controlled by [1] followed by pauliX on [1] controlled by [0] followed by hadamard on [0]
 * measure [0, 1]
 * pauliX on [2] controlled by [1] followed by pauliZ on [2] controlled by [0]
          ----------
   0 ----| COMPILED |---[ / ]------- X ---------
         |          |
         |          |
   1 ----| COMPILED |---[ / ]------- X ---------
         |          |
         |          |            ----------
   2 ----| COMPILED |-----------| COMPILED |----
          ----------             ----------
Initial states: 000 (-0.131496 + 0.329310 i), 100 (0.892845 + 0.277653 i)
   Alice: 0 (12.57 %) / 1 (87.43 %)
Final states: 000 (-0.131496 + 0.329310 i), 001 (0.892845 + 0.277653 i)
   Bob  : 0 (12.57 %) / 1 (87.43 %)

Custom Gate (Fredkin Example)

print('');
print('USING STANDARD GATES');

final fredkinCircuitWithStandardGates = QCircuit(size: 3);
fredkinCircuitWithStandardGates.controlledNot(2, 1);
fredkinCircuitWithStandardGates.toffoli(0, 1, 2);
fredkinCircuitWithStandardGates.controlledNot(2, 1);

describe(fredkinCircuitWithStandardGates);
draw(fredkinCircuitWithStandardGates);
verifyFredkin(fredkinCircuitWithStandardGates);

print('');
print('USING CUSTOM GATE');

final cnot21 = QGates.controlled(3).not(2, 1);
final toffoli012 = QGates.highLevel(3).toffoli(0, 1, 2);
// Here, the custom Fredkin gate matrix is computed by multiplying
// the matrices of the standard gates that make it up.
// The Fredkin matrix (hard-coded) could have been provided as well.
final myFredkinGate = cnot21 * toffoli012 * cnot21;
final fredkinType = QGateType('My Fredkin gate', 'MY-C-SWAP');
final fredkinCircuitWithCustomGate = QCircuit(size: 3);
fredkinCircuitWithCustomGate.custom({1, 2}, myFredkinGate, controls: {0}, type: fredkinType);

print(myFredkinGate.toStringIndent(1));
describe(fredkinCircuitWithCustomGate);
draw(fredkinCircuitWithCustomGate);
verifyFredkin(fredkinCircuitWithCustomGate);

print('');
print('USING BUILT-IN GATE');

final fredkinCircuitWithBuiltInGate = QCircuit(size: 3);
fredkinCircuitWithBuiltInGate.fredkin(0, 1, 2);

describe(fredkinCircuitWithBuiltInGate);
draw(fredkinCircuitWithBuiltInGate);
verifyFredkin(fredkinCircuitWithBuiltInGate);

输出:

USING STANDARD GATES
 * pauliX on [1] controlled by [2]       
 * toffoli on [2] controlled by [0, 1]   
 * pauliX on [1] controlled by [2]       

   0 ================ X =================

          -----                -----
   1 ====| NOT |===== X ======| NOT |====
          -----                -----
                   --------
   2 ====== X ====| CC-NOT |==== X ======
                   --------
Initial: 000 (100 %) => Outcome: 000 (100 %): OK
Initial: 001 (100 %) => Outcome: 001 (100 %): OK
Initial: 010 (100 %) => Outcome: 010 (100 %): OK
Initial: 011 (100 %) => Outcome: 011 (100 %): OK
Initial: 100 (100 %) => Outcome: 100 (100 %): OK
Initial: 101 (100 %) => Outcome: 110 (100 %): OK
Initial: 110 (100 %) => Outcome: 101 (100 %): OK
Initial: 111 (100 %) => Outcome: 111 (100 %): OK

USING CUSTOM GATE
   [
      [1, 0, 0, 0, 0, 0, 0, 0],
      [0, 1, 0, 0, 0, 0, 0, 0],
      [0, 0, 1, 0, 0, 0, 0, 0],
      [0, 0, 0, 1, 0, 0, 0, 0],
      [0, 0, 0, 0, 1, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 1, 0],
      [0, 0, 0, 0, 0, 1, 0, 0],
      [0, 0, 0, 0, 0, 0, 0, 1]
   ]
 * My Fredkin gate on [1, 2] controlled by [0]

   0 ========= X =========

          -----------
   1 ====| MY-C-SWAP |====
         |           |    
         |           |
   2 ====| MY-C-SWAP |====
          -----------
Initial: 000 (100 %) => Outcome: 000 (100 %): OK
Initial: 001 (100 %) => Outcome: 001 (100 %): OK
Initial: 010 (100 %) => Outcome: 010 (100 %): OK
Initial: 011 (100 %) => Outcome: 011 (100 %): OK
Initial: 100 (100 %) => Outcome: 100 (100 %): OK
Initial: 101 (100 %) => Outcome: 110 (100 %): OK
Initial: 110 (100 %) => Outcome: 101 (100 %): OK
Initial: 111 (100 %) => Outcome: 111 (100 %): OK

USING BUILT-IN GATE
 * fredkin on [1, 2] controlled by [0]

   0 ======= X ========

          --------
   1 ====| C-SWAP |====
         |        |
         |        |
   2 ====| C-SWAP |====
          --------
Initial: 000 (100 %) => Outcome: 000 (100 %): OK
Initial: 001 (100 %) => Outcome: 001 (100 %): OK
Initial: 010 (100 %) => Outcome: 010 (100 %): OK
Initial: 011 (100 %) => Outcome: 011 (100 %): OK
Initial: 100 (100 %) => Outcome: 100 (100 %): OK
Initial: 101 (100 %) => Outcome: 110 (100 %): OK
Initial: 110 (100 %) => Outcome: 101 (100 %): OK
Initial: 111 (100 %) => Outcome: 111 (100 %): OK

更多关于Flutter插件qartvm的使用_Dart和Flutter的量子计算模拟包的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter插件qartvm的使用_Dart和Flutter的量子计算模拟包的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


探索和使用Flutter中的未知功能插件,特别是像qartvm这样不太常见的插件,需要一定的谨慎,因为这类插件可能缺乏官方文档和社区支持。不过,我们可以通过查看插件的源代码、示例代码或者GitHub仓库来获取使用指南。假设qartvm是一个提供特定功能的Flutter插件(由于这是一个假设的插件名,以下代码将根据一般Flutter插件的使用模式进行构造),以下是一个探索和使用该插件的基本步骤和示例代码。

步骤 1: 添加依赖

首先,你需要在pubspec.yaml文件中添加对qartvm插件的依赖。请注意,由于这是一个假设的插件名,你需要替换为实际的插件名和版本号。

dependencies:
  flutter:
    sdk: flutter
  qartvm: ^x.y.z  # 替换为实际的版本号

然后运行flutter pub get来安装依赖。

步骤 2: 导入插件

在你的Dart文件中导入该插件。

import 'package:qartvm/qartvm.dart';

步骤 3: 使用插件功能

由于qartvm是一个假设的插件名,我们不知道它具体提供了哪些功能。但通常,Flutter插件会提供一些类和方法来执行其特定的功能。以下是一个假设性的示例,展示如何使用一个假想的QartVM类来执行某些操作。

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('QartVM Example'),
        ),
        body: Center(
          child: QartVMExample(),
        ),
      ),
    );
  }
}

class QartVMExample extends StatefulWidget {
  @override
  _QartVMExampleState createState() => _QartVMExampleState();
}

class _QartVMExampleState extends State<QartVMExample> {
  String result = '';

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        Text('QartVM Result: $result'),
        ElevatedButton(
          onPressed: () async {
            // 假设QartVM有一个名为execute的方法,它返回一个Future<String>
            result = await QartVM.execute('some input');
            setState(() {});
          },
          child: Text('Execute QartVM'),
        ),
      ],
    );
  }
}

在这个示例中,我们假设QartVM类有一个静态方法execute,它接受一个字符串参数并返回一个Future<String>。当用户点击按钮时,该方法被调用,并且结果更新到UI上。

注意事项

  1. 文档和示例:如果qartvm是一个真实存在的插件,最好查看其官方文档和示例代码来了解其确切的用法和功能。
  2. 错误处理:在实际应用中,你应该添加适当的错误处理逻辑来处理可能的异常情况。
  3. 社区支持:如果插件缺乏文档,你可以尝试在Stack Overflow、GitHub Issues或Flutter社区论坛中寻求帮助。

由于qartvm是一个假设的插件名,以上代码仅作为探索和使用未知Flutter插件的一般指南。在实际应用中,你需要根据插件的实际功能和API进行调整。

回到顶部