0
$\begingroup$

I am new to qiskit and currently trying to implement Shor's 9-Qubit_Code. I am trying to orient my code on the example given in the official qiskit-tutorial and so far I have the following:

import matplotlib.pyplot as plt
import numpy as np
import math
from qiskit import *

circuit = QuantumCircuit(9)

circuit.cnot(0, 3)

circuit.cnot(0, 6)

circuit.h(0)
circuit.h(3)
circuit.h(6)

circuit.cnot(0, 1)
circuit.cnot(3, 4)
circuit.cnot(6, 7)

circuit.cnot(0, 2)
circuit.cnot(3, 5)
circuit.cnot(6, 8)

# Insert some error here
circuit.barrier()
circuit.x(0)
circuit.id(1)
circuit.id(2)
circuit.id(3)
circuit.id(4)
circuit.id(5)
circuit.id(6)
circuit.id(7)
circuit.id(8)
circuit.barrier()

circuit.cnot(0, 1)
circuit.cnot(3, 4)
circuit.cnot(6, 7)

circuit.cnot(0, 2)
circuit.cnot(3, 5)
circuit.cnot(6, 8)

circuit.ccx(2, 1, 0)
circuit.ccx(5, 4, 3)
circuit.ccx(8, 7, 6)

circuit.h(0)
circuit.h(3)
circuit.h(6)

circuit.cnot(0, 3)

circuit.cnot(0, 6)

circuit.ccx(8, 3, 0)

circuit.draw(output='mpl')
plt.show()



# Run the quantum circuit on a statevector simulator backend
backend = Aer.get_backend('statevector_simulator')
# Create a Quantum Program for execution
job = backend.run(circuit)
result = job.result()
outputstate = result.get_statevector(circuit, decimals=3)
print(outputstate)

However, I still need to be able to set the first qubit in an arbitrary state, but I do not know how to do this and I can not find something like that on the tutorials page. Could you please help me?

Remark: This is not supposed to be a "serious" program, it is just for me to get a feel of what is going on.

$\endgroup$
2
  • $\begingroup$ You can use the qiskit.circuit.Parameter class to define a parametrised state preparation. $\endgroup$ Commented Jun 7, 2023 at 15:44
  • $\begingroup$ You can also just use the rotation gates on the first qubit to prepare random states. A RX RZ RX sequence can prepare a single qubit in an arbitrary state. $\endgroup$ Commented Jun 7, 2023 at 18:53

1 Answer 1

1
$\begingroup$

I didn't check all of your code, but this answer addresses the following:

I still need to be able to set the first qubit in an arbitrary state

To do this, you can use the quantum_info.random_statevector function to get a random 1-qubit vector (this vector has dimension 2 so that is why we pass the argument 2 to the function) from the uniform Haar measure.

Then, you initialize your first qubit (index 0) to this vector using the QuantumCircuit.initialize method. This method takes as its first argument the vector to initialize to and as its second argument the index of the qubit to initialize.

import qiskit
import numpy as np

sv = qiskit.quantum_info.random_statevector(2)
qc = qiskit.QuantumCircuit(1)
qc.initialize(sv.data, 0)
   ┌─────────────────────────────────────────────────┐
q: ┤ Initialize(0.95024-0.10594j,-0.067802+0.28499j) ├
   └─────────────────────────────────────────────────┘
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.