模拟过程中出现错误:“实验没有计数

问题描述 投票:0回答:1

将 numpy 导入为 np

从 qiskit 导入 QuantumCircuit,转译

从 qiskit_aer 导入 AerSimulator

导入子流程

导入螺纹

def create_quantum_电路():

# Create a quantum circuit with 3 qubits

qc = QuantumCircuit(3)



# Generate superposition

qc.h(0)



# Add quantum phase

qc.p(np.pi/2, 0)



# Controlled-NOT gates

qc.cx(0, 1)

qc.cx(0, 2)



return qc

定义模拟量子电路(qc):

# Simulate the circuit

backend = AerSimulator()

transpiled_circuit = transpile(qc, backend)



while True:

    try:

        print("Transpiled circuit:")

        print(transpiled_circuit)

        

        result = backend.run(transpiled_circuit).result()

        counts = result.get_counts(transpiled_circuit)

        yield counts

    except Exception as e:

        print("Error during simulation:", e)

defexecute_linux_program():

# Prompt the user for the Linux command

command = input("Enter the Linux command to execute inside the quantum environment: ")



# Execute the provided command in a separate thread

def run_command():

    try:

        output = subprocess.check_output(command, shell=True, universal_newlines=True)

        print("Output of the command:")

        print(output)

    except subprocess.CalledProcessError as e:

        print("Error executing the command:", e)



thread = threading.Thread(target=run_command)

thread.start()

thread.join()

def main():

# Create the quantum circuit

quantum_circuit = create_quantum_circuit()



# Check if the circuit is empty or not properly defined

if quantum_circuit.width() == 0:

    print("Quantum circuit is empty or not properly defined.")

    return



# Continuously simulate and print the quantum circuit

simulator = simulate_quantum_circuit(quantum_circuit)

while True:

    try:

        counts = next(simulator)

        print("Quantum simulation results:", counts)

    except StopIteration:

        print("Simulation stopped.")

        break

    

    # Execute the Linux program (replace with your logic)

    execute_linux_program()

if name == "main":

main()

输出:

模拟过程中出现错误:“实验“”没有计数”转译电路:┌────┐┌────────┐ q_0: ┤ H ├┤ P(π/2) ├──■── ──■── └────┘└────────┘┌─┴─┐ │ q_1: ────────────────┤ X ├──┼─ ─ └───┘┌─┴─┐ q_2: ────────────────────┤ X ├ └───┘

预期的可持续性

quantum-computing
1个回答
0
投票

您遗漏的一件重要事情是,电路中的量子位没有被测量。如果您的电路中不包含测量,则

AerSimulator()
将无法获得计数(或样本)。要解决此问题,只需将测量块添加到您的电路中即可。这是更正后的代码:

import numpy as np
from qiskit import QuantumCircuit, transpile 
from qiskit_aer import AerSimulator

qc = QuantumCircuit(3)
# Generate superposition
qc.h(0)
# Add quantum phase
qc.p(np.pi/2, 0)

# Controlled-NOT gates
qc.cx(0, 1) qc.cx(0, 2)

# Measure all qubits 
qc.measure_all()    # This is the line that you missed

# Simulate the circuit
backend = AerSimulator()
transpiled_circuit = transpile(qc, backend)
result = backend.run(transpiled_circuit).result() 
counts = result.get_counts(transpiled_circuit)

print(counts)

这会生成以下输出:

{'000': 514, '111': 510}
© www.soinside.com 2019 - 2024. All rights reserved.