In this example, I'll use Qiskit, a popular Python library for working with quantum circuits. Make sure you have Qiskit installed (pip install qiskit).
Code: Select all
from qiskit import QuantumCircuit, transpile, assemble, Aer, execute
from qiskit.visualization import plot_bloch_multivector, plot_histogram
# Create a 3-qubit quantum circuit
qc = QuantumCircuit(3)
# Apply the controlled-Z gate
qc.cz(0, 2) # First qubit is the control, last qubit is the target
# Visualize the circuit
print(qc)
# Simulate the circuit using a state vector simulator
simulator = Aer.get_backend('statevector_simulator')
job = execute(qc, simulator)
result = job.result()
statevector = result.get_statevector()
# Print the final state vector
print("Final state vector:")
print(statevector)
If you want to construct a multi-qubit controlled-Z gate with more control qubits, you can use additional single-qubit and two-qubit gates to implement the necessary operations. For example, to implement a 4-qubit controlled-Z gate, you can use the following steps:
- Apply a Hadamard gate (H gate) to the target qubit.
- Apply a Toffoli gate (CCX gate) with the first two qubits as control qubits and the third qubit as the target qubit.
- Apply a Toffoli gate (CCX gate) with the result of the previous Toffoli gate and the fourth qubit as control qubits and the target qubit as the third qubit.
- Apply another Hadamard gate (H gate) to the target qubit.
This sequence of gates effectively creates a 4-qubit controlled-Z gate.