ubsuny / 23-Homework1G1

Homework 1 Repository Group 1
0 stars 6 forks source link

Add docstring into .py file #45

Open s4il3sh opened 12 months ago

s4il3sh commented 12 months ago

Hi G1 team,

I am working to add a docstring to the quantum_addition.py file but it throws me some errors. Could you please show me a hint of how to do it? Also, I have no idea how to check the docstring if it is really working. For example, here is a part of the code:

  qc = QuantumCircuit(num_qubits * 2, num_qubits) # Here, a quantum circuit (qc) is created with twice the number 
                                                    # of qubits as the sum, plus an additional num_qubits for the output.

    # Encode the classical numbers into quantum states
    for i in range(num_qubits):
        if (num1 >> i) & 1:
            qc.x(i)  # Apply X gate for 1 bits in num1
        if (num2 >> i) & 1:
            qc.x(i + num_qubits)  # Apply X gate for 1 bits in num2
 # This loop encodes the binary representation of num1 and num2 into the quantum circuit 
# by applying an X gate to each qubit where the corresponding bit is set to 1.

    # Perform the addition by applying CNOT gates
    for i in range(num_qubits - 1):
        qc.ccx(i, i + num_qubits, i + num_qubits + 1)
        qc.cx(i, i + num_qubits)

    # Measure the result
    qc.measure(range(num_qubits, num_qubits * 2), range(num_qubits))
#It measures the qubits in the output range and maps the 
# measurement results back to classical bits.

    # Simulate the circuit
    simulator = Aer.get_backend('qasm_simulator')
    compiled_circuit = transpile(qc, simulator)
    job = execute(compiled_circuit, simulator, shots=shots)  # Specify the number of shots
    result = job.result()

    # Get the measurement result
    counts = result.get_counts(qc)
    result_decimal = int(list(counts.keys())[0], 2)
#This code retrieves the measurement counts and converts 
# the binary measurement result to a decimal integer.  
    return result_decimal, counts  # Return both the result and the counts

Any idea how to add docstring for QuantumCircuit?