Jawadkotaich/quanbench-plus huggingface dataset:
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
def grover_search_oracle_00() ->QuantumCircuit:
    """
        Implement a quantum circuit using grover algorithm to find oracle |00> using qiskit, return the QuantumCircuit after measuring.
    """

Framework: qiskit | Category: QUANTUM_ALGO

Solution:
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
def grover_search_oracle_00() ->QuantumCircuit:
    
    qr = QuantumRegister(2, 'q')
    cr = ClassicalRegister(2, 'c')
    qc = QuantumCircuit(qr, cr)

    qc.h([0, 1])
    qc.x([0, 1])
    qc.cz(0, 1)
    qc.x([0, 1])
    qc.h([0, 1])
    qc.z([0, 1])
    qc.cz(0, 1)
    qc.x([0, 1])
    qc.h([0, 1])
        
    qc.measure([0, 1], [0, 1])
   
    return qc
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
def generate_quantum_state_qubit3() ->QuantumCircuit: 

        """
                Implementation a quantum circuit to create the state ψ = √(1/2)(|011⟩ − |100⟩) using qiskit, return the QuantumCircuit after measuring.
        """

Framework: qiskit | Category: STATE_PREPARATION

Solution:
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit

def generate_quantum_state_qubit3() -> QuantumCircuit:
    qc = QuantumCircuit(3,3)   
    qc.h(2)     
    qc.x(1)     
    qc.cx(2,1)  
    qc.cx(1,0)
    qc.z(2) 
    qc.measure([0,1,2], [0,1,2])
    
    return qc
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
def grover_3SAT() ->QuantumCircuit: 

        """
                Implement a quantum circuit using grover algorithm to solve the 3 SAT problem with 3-CNF formula {((x1) | (x2) | (x3))
                & ((_not(x1)) | (x2) | (x3))
                & ((_not(x1)) | (_not(x2)) | (_not(x3)))
                & ((_not(x1)) | (_not(x2)) | (x3))
                & ((x1) | (x2) | (_not(x3)))
                & ((_not(x1)) | (x2) | (_not(x3)))} using qiskit, return the QuantumCircuit after measuring.
        """

Framework: qiskit | Category: QUANTUM_ALGO

Solution:
from qiskit.circuit.library import PhaseOracle
from qiskit_algorithms import Grover, AmplificationProblem
from qiskit.primitives import Sampler

def grover_3SAT():
    """
    Solve 3-SAT problem using Grover's Algorithm (compatible with new Qiskit version).
    Formula:
    ((x1) ∨ (x2) ∨ (x3)) ∧
    (¬x1 ∨ x2 ∨ x3) ∧
    (¬x1 ∨ ¬x2 ∨ ¬x3) ∧
    (¬x1 ∨ ¬x2 ∨ x3) ∧
    (x1 ∨ x2 ∨ ¬x3) ∧
    (¬x1 ∨ x2 ∨ ¬x3)
    """
    expression = "(x1 | x2 | x3)&(~x1 | x2 | x3)&(~x1 | ~x2 | ~x3)&(~x1 | ~x2 | x3)&(x1 | x2 | ~x3)&(~x1 | x2 | ~x3)"
    
    oracle = PhaseOracle(expression)
    problem = AmplificationProblem(oracle=oracle)
    grover = Grover(sampler=Sampler(options={"shots": 10000}))
    result = grover.amplify(problem)

    return result.circuit_results[0]
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
def qaoa_maxcut_ansatz(G, beta, gamma) ->QuantumCircuit: 

        """
                Implement a quantum circuit using QAOA algorithm to solve the maxcut problem with the graph ([[0,3],[0,4],[1,3],[1,4],[2,3],[2,4]]), return the QuantumCircuit after measuring.
                 Parameters:
                        G (networkx.Graph): The input graph for MaxCut.
                        beta (List[float]): A list of 5 parameters for the mixing unitaries (RX rotations).
                        gamma (List[float]): A list of 5 parameters for the cost unitaries (ZZ rotations).
                        
        """

Framework: qiskit | Category: QUANTUM_ALGO

Solution:
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister

def qaoa_maxcut_ansatz(G, beta, gamma) -> QuantumCircuit:
    
    n = len(G.nodes)
    p = len(beta)
    
    qr = QuantumRegister(n, 'q')
    cr = ClassicalRegister(n, 'c')
    qc = QuantumCircuit(qr, cr)
    qc.h(qr)
    for i in range(p):
        
        for u, v in G.edges:
            qc.cx(qr[u], qr[v])
            qc.rz(2 * gamma[i], qr[v])
            qc.cx(qr[u], qr[v])
        
        
        for qubit in range(n):
            qc.rx(2 * beta[i], qr[qubit])
    
    
    qc.measure(qr, cr)
    
    return qc
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
def swaptest_zaxis(unknown_state: QuantumCircuit) ->QuantumCircuit: 

    """
            Implement a quantum circuit using the SWAP test algorithm to estimate the angle θ (in radians) between an unknown single-qubit quantum state and the |0⟩ state (Z-axis).
            The input is a single qubit QuantumCircuit unknown_state, and returns a QuantumCircuit after measuring the ancilla qubit
    """

Framework: qiskit | Category: QUANTUM_ALGO

Solution:
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
def swaptest_zaxis(unknown_state: QuantumCircuit) ->QuantumCircuit: 

    qr = QuantumRegister(3, 'q')  
    cr = ClassicalRegister(1, 'c') 
    qc = QuantumCircuit(qr, cr)
    
    qc = qc.compose(unknown_state, [qr[1]])  
    qc.h(qr[0])
    qc.cswap(qr[0], qr[1], qr[2]) 
    qc.h(qr[0])
    qc.measure(qr[0], cr[0])
    return qc
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
def swaptest_individual() ->QuantumCircuit: 
    
    """
        Implement a quantum circuit using the SWAP test with individual ancilla qubits for each qubit-to-qubit comparison between two 3-qubit quantum states: |011⟩ and |000⟩.
        Encode |011⟩ on the first set of 3 qubits, and |000⟩ on the second set of 3 qubits. Use 3 ancilla qubits and measure ancilla qubits.
    """

Framework: qiskit | Category: QUANTUM_ALGO

Solution:
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
def swaptest_individual() ->QuantumCircuit: 
    qr = QuantumRegister(9, 'q')
    cr = ClassicalRegister(3, 'c')
    qc = QuantumCircuit(qr, cr)

    qc.x(qr[1])
    qc.x(qr[2])
    
    qc.h(qr[6])
    qc.h(qr[7])
    qc.h(qr[8])
    qc.cswap(qr[6],qr[0], qr[3])
    qc.cswap(qr[7],qr[1], qr[4])
    qc.cswap(qr[8], qr[2], qr[5])
    qc.h(qr[7])
    qc.h(qr[6])
    qc.h(qr[8])
    qc.measure(qr[6], cr[0])
    qc.measure(qr[7], cr[1])
    qc.measure(qr[8], cr[2])
    return qc
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from math import pi

def qft_6() ->QuantumCircuit: 

    """
        Implement a 6 qubit quantum fourier transform circuit, and return the QuantumCircuit after measure all qubits.
    """

Framework: qiskit | Category: QUANTUM_ALGO

Solution:
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from math import pi

def qft_6():
    """QFT on 6 qubits"""
    qr = QuantumRegister(6, 'q')
    cr = ClassicalRegister(6, 'c')
    qc = QuantumCircuit(qr,cr)
    for i in range(5, -1, -1):
        qc.h(qr[i])
        for j in range(i):
            qc.cp(pi/2**(i-j), qr[j], qr[i])
    
    for j in range(3): 
        qc.swap(qr[j], qr[5-j])

    qc.measure(qr, cr)
    return qc
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
def qpe_x_gate() ->QuantumCircuit: 
    
    """
        Implement a quantum circuit to perform Quantum Phase Estimation using 3 counting qubits and 1 target qubit to estimate the phase of a unitary operator X (Pauli-X gate).
        Return a QuantumCircuit and measure the counting qubits.
    """

Framework: qiskit | Category: QUANTUM_ALGO

Solution:
from qiskit import QuantumCircuit
from qiskit.circuit.library import QFT

def qpe_x_gate(n_count=3):
    qc = QuantumCircuit(n_count + 1, n_count)
    qc.h(range(n_count))
    qc.x(n_count)

    for qubit in range(n_count):
        repetitions = 2**qubit
        for _ in range(repetitions):
            qc.cx(qubit, n_count) 

    qc.append(QFT(n_count, inverse=True, do_swaps=True).to_gate(), range(n_count))

    qc.measure(range(n_count), range(n_count))

    return qc

print(qpe_x_gate())
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister


def Deutsch_Jozsa_Balance_4() ->QuantumCircuit: 
    """
        Implement a quantum circuit use DeutschJozsa algorithm to solve a oracle with bitstring "1100", return the QuantumCircuit after measure all qubits.
    """

Framework: qiskit | Category: QUANTUM_ALGO

Solution:
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister
import numpy as np

def Deutsch_Jozsa_Balance_4() -> QuantumCircuit:
    def dj_oracle(case, n):

        oracle_qc = QuantumCircuit(n+1)
        if case == "balanced":
            b = np.random.randint(1,2**n)
            b_str = '1100'
            for qubit in range(len(b_str)):
                if b_str[qubit] == '1':
                    oracle_qc.x(qubit)
            for qubit in range(n):
                oracle_qc.cx(qubit, n)
            for qubit in range(len(b_str)):
                if b_str[qubit] == '1':
                    oracle_qc.x(qubit)
        if case == "constant":
            output = np.random.randint(2)
            if output == 1:
                oracle_qc.x(n)
        
        oracle_gate = oracle_qc.to_gate() 
        return oracle_gate

    def dj_algorithm(oracle, n):
        dj_circuit = QuantumCircuit(n+1, n)
        dj_circuit.x(n)
        dj_circuit.h(n)
        for qubit in range(n):
            dj_circuit.h(qubit)
        dj_circuit.append(oracle, range(n+1))
        for qubit in range(n):
            dj_circuit.h(qubit)
        for i in range(n):
            dj_circuit.measure(i, i)
        return dj_circuit
    
    n = 4  
    oracle_gate = dj_oracle('balanced', n)
    dj_circuit = dj_algorithm(oracle_gate, n)
    
    return dj_circuit
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister


def Deutsch_Jozsa_Constant_4() ->QuantumCircuit: 
    """
        Implement a quantum circuit use DeutschJozsa algorithm to solve a oracle with bitstring "0000", return the QuantumCircuit after measure all qubits.
    """

Framework: qiskit | Category: QUANTUM_ALGO

Solution:
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister
import numpy as np

def Deutsch_Jozsa_Constant_4() -> QuantumCircuit:
    def dj_oracle(case, n):
        oracle_qc = QuantumCircuit(n+1)
        if case == "balanced":
            b = np.random.randint(1,2**n)
            b_str = '1100'
            for qubit in range(len(b_str)):
                if b_str[qubit] == '1':
                    oracle_qc.x(qubit)
            for qubit in range(n):
                oracle_qc.cx(qubit, n)
            for qubit in range(len(b_str)):
                if b_str[qubit] == '1':
                    oracle_qc.x(qubit)
        if case == "constant":
            output = 0
            if output == 1:
                oracle_qc.x(n)
        oracle_gate = oracle_qc.to_gate() 
        return oracle_gate

    def dj_algorithm(oracle, n):
        dj_circuit = QuantumCircuit(n+1, n)
        dj_circuit.x(n)
        dj_circuit.h(n)
        for qubit in range(n):
            dj_circuit.h(qubit)
        dj_circuit.append(oracle, range(n+1))
        for qubit in range(n):
            dj_circuit.h(qubit)
        for i in range(n):
            dj_circuit.measure(i, i)
        return dj_circuit

    n = 4  
    oracle_gate = dj_oracle('constant', n)
    dj_circuit = dj_algorithm(oracle_gate, n)
    
    return dj_circuit
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister

def Simon_11() ->QuantumCircuit: 
    """
        Implement a quantum circuit using Simon's algorithm for the case where:
            - The input bit length n = 2
            - The hidden string s = '11'
        Return a QuantumCircuit after measure the qubit about s.
    """

Framework: qiskit | Category: QUANTUM_ALGO

Solution:
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister

def Simon_11() ->QuantumCircuit: 
    qc = QuantumCircuit(4,2)
    qc.h(0)
    qc.h(1)
    for i in range(2):
        qc.cx(i,2)
        qc.cx(i,3)
        qc.h(i)
    qc.measure(2,0)
    qc.measure(3,1)
    
    return qc
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister

def Simon_110() ->QuantumCircuit: 
    """
        Implement a quantum circuit using Simon's algorithm for the case:
            - The input bit length n = 3
            - The hidden string s = '110'
        Return the QuantumCircuit after measure qubits about s.
    """

Framework: qiskit | Category: QUANTUM_ALGO

Solution:
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister

def Simon_110() ->QuantumCircuit: 
    qc = QuantumCircuit(6,3)
    for i in range(3):
        qc.h(i)
    for i in range(3):
        qc.cx(i,i+3)
    qc.cx(1,4)
    qc.cx(1,5)
    for i in range(3):
        qc.h(i)
        qc.measure(i,i)
    return qc
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister

def Hadmard_Test_h() ->QuantumCircuit: 
    """
        Implement a 2 qubit Hadmard test quantum circuit for |+> state, return the QuantumCircuit after measure the ancilla qubit.
    """

Framework: qiskit | Category: QUANTUM_ALGO

Solution:
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister

def Hadmard_Test_h() ->QuantumCircuit: 
    qc = QuantumCircuit(2,1)
    qc.h(0)
    qc.h(1)
    qc.ch(0,1)
    qc.h(0)
    qc.measure(0,0)
    
    return qc
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister

def Bell_State() ->QuantumCircuit: 
    """
        Implement a quantum circuit with Bell State, return the QuantumCircuit after measure all qubits.
    """

Framework: qiskit | Category: QUANTUM_ALGO

Solution:
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister

def Bell_State() ->QuantumCircuit: 
    qc = QuantumCircuit(2,2)
    qc.h(0)
    qc.cx(0,1)
    qc.measure(0,0)
    qc.measure(1,1)
    return qc
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister

def GHZ() ->QuantumCircuit: 
    """
        Implement a 3 qubits GHZ state quantum circuit, return the QuantumCircuit after measure all qubits. 
    """

Framework: qiskit | Category: QUANTUM_ALGO

Solution:
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister

def GHZ() ->QuantumCircuit: 
    qc = QuantumCircuit(3,3)
    qc.h(0)
    qc.cx(0,1)
    qc.cx(1,2)
    qc.measure([0,1,2],[0,1,2])
    
    return qc
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister
from numpy import pi
def Quantum_Teleportation() ->QuantumCircuit: 
    """
        Implement a 3 qubits Quantum_Teleportation, Alice own Qubit 0 and Qubit 1, Bob own Qubit2. Alice's initial state is rotated by angle pi/2 around the X axis. 
        Return QuantumCircuit after just measure Bob's qubit.
    """

Framework: qiskit | Category: QUANTUM_ALGO

Solution:
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister
from numpy import pi
def Quantum_Teleportation() ->QuantumCircuit: 
    qc = QuantumCircuit(3,1)
    qc.rx(pi/2,0)
    qc.h(1)
    qc.cx(1,2)
    qc.cx(0,1)
    qc.h(0)
    qc.cx(1,2)
    qc.cz(0,2)
    qc.measure(2,0)

    return qc
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
def generate_quantum_state_qubit5() ->QuantumCircuit: 

        """
                implementation a quantum circuit to create the state ψ = √(1/2)(|00110⟩ + |00101⟩) using qiskit, return the QuantumCircuit after measure all qubits.
        """

Framework: qiskit | Category: QUANTUM_ALGO

Solution:
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
def generate_quantum_state_qubit5() ->QuantumCircuit: 

    qc = QuantumCircuit(5,5)
    qc.x(2)
    qc.h(3)
    qc.cx(3,4)
    qc.x(4)
    qc.measure([0,1,2,3,4],[4,3,2,1,0])
    return qc
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister

def Bernstein_Vazirani_011() ->QuantumCircuit: 
    """
        Implement the Bernstein–Vazirani algorithm for a 3-bit hidden string a = '011'. 
        Return the QuantumCircuit after measure all qubits.
    """

Framework: qiskit | Category: QUANTUM_ALGO

Solution:
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister

def Bernstein_Vazirani_011() ->QuantumCircuit: 
    qc = QuantumCircuit(4,3)
    qc.x(3)
    qc.h([0,1,2,3])
    qc.cx(1,3)
    qc.cx(2,3)
    qc.h([0,1,2])
    qc.measure([0,1,2],[0,1,2])
    return qc
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister

def MAJ_3() ->QuantumCircuit: 
    """
        Implement a 3-input quantum Majority (MAJ) gate using CNOT and CCNOT (Toffoli) gates. 
        The circuit takes as input three qubits:
            - c_i: the carry bit from the previous stage
            - b_i: one of the current bits to add
            - a_i: the other current bit to add
        The MAJ gate performs the following transformations (reversible logic only):
            - Output 1: a_i ⊕ b_i 
            - Output 2: a_i ⊕ c_i 
            - Output 3: c_{i+1}, the carry-out of the majority function over a_i, b_i, and c_i
        Return the QuantumCircuit after measure all qubits.
    """

Framework: qiskit | Category: QUANTUM_ALGO

Solution:
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister

def MAJ_3() ->QuantumCircuit: 
    qc = QuantumCircuit(3,3)
    qc.cx(0, 1)
    qc.cx(0, 2)
    qc.ccx(1, 2, 0)
    qc.measure([0, 1, 2], [0, 1, 2])
    return qc
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister

def Qadd() ->QuantumCircuit: 
    """
        Implement a quantum Adder for a = 100, b = 001, and c0 = 1. Return the QuantumCircuit after measure the result qubits (4 qubits).
    """

Framework: qiskit | Category: QUANTUM_ALGO

Solution:
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister

def Qadd() -> QuantumCircuit:
    qc = QuantumCircuit(8, 4)
    qc.x(0)
    qc.x(1)
    qc.x(6)
    def MAJ(qc,ci,bi,ai):
        qc.cx(ai,bi)
        qc.cx(ai,ci)
        qc.ccx(bi,ci,ai)
        return qc
    def UMA(qc, ci, bi, ai):
        qc.ccx(bi,ci,ai)
        qc.cx(ai,ci)
        qc.cx(ci,bi)
        return qc
    for i in range(3):
        MAJ(qc,2*i,2*i+1,2*i+2)
    qc.cx(6,7)
    for i in range(2,-1,-1):
        UMA(qc,2*i,2*i+1,2*i+2)
    qc.measure([1,3,5,7],[0,1,2,3])
    return qc
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister
def QSub() ->QuantumCircuit: 
    """
        Implement a 3 qubits quantum subtractor for a = 111, b = 011. Return the QuantumCircuit after measure the result qubits.
    """

Framework: qiskit | Category: QUANTUM_ALGO

Solution:
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister
def QSub(cir = QuantumCircuit(8,3), a = '111', b = '011') ->QuantumCircuit: 
    def to_twos_complement(binary_str):
        n = len(binary_str)
        inverted = ''.join('1' if x == '0' else '0' for x in binary_str)
        inverted_as_int = int(inverted, 2) + 1
        twos_complement = bin(inverted_as_int)[-n:]
        if len(twos_complement) < n:
            twos_complement = '0' * (n - len(twos_complement)) + twos_complement
        return twos_complement

    def MAJ(qc,ci,bi,ai):
        qc.cx(ai,bi)
        qc.cx(ai,ci)
        qc.ccx(bi,ci,ai)
        return qc
    def UMA(qc, ci, bi, ai):
        qc.ccx(bi,ci,ai)
        qc.cx(ai,ci)
        qc.cx(ci,bi)
        return qc
    
    b=to_twos_complement(b)
    for i,s in enumerate(a[::-1]):
        if s=='1':
            cir.x(2*i+2)
    for i,s in enumerate(b[::-1]):
        if s=='1':
            cir.x(2*i+1)
    l=len(a)
    cir.barrier()
    for i in range(l):
        MAJ(cir,2*i,2*i+1,2*i+2)
        cir.barrier()
    cir.cx(len(a)+len(b),len(a)+len(b)+1)
    for i in range(l-1,-1,-1):
        cir.barrier()
        UMA(cir,2*i,2*i+1,2*i+2)
    cir.measure([1,3,5],[0,1,2])
    return cir

if __name__ == "__main__":
    a='111'
    b='011' 

    qubit_num=len(a)+len(b)+2
    #cir = QuantumCircuit(qubit_num)
    cir = QSub()
    print(cir)
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister

def W_State_4() ->QuantumCircuit: 
    """
        Implement a 4 qubits W state quantum circuit. Return the QuantumCircuit after measure all qubits. 
    """

Framework: qiskit | Category: QUANTUM_ALGO

Solution:
from qiskit import QuantumCircuit
from qiskit.circuit.library import UnitaryGate
import numpy as np

def W_State_4() -> QuantumCircuit:
    def RBSGate(theta):
        rbs_matrix = np.array([
            [1, 0,          0,         0],
            [0, np.cos(theta), -np.sin(theta), 0],
            [0, np.sin(theta),  np.cos(theta), 0],
            [0, 0,          0,         1]
        ])
        return UnitaryGate(rbs_matrix, label="RBS")
    
    qc = QuantumCircuit(4,4)    
    qc.x(0)
    qc.append(RBSGate(np.arccos(1/np.sqrt(4))), [0, 1])
    qc.append(RBSGate(np.arccos(1/np.sqrt(3))), [1, 2])
    qc.append(RBSGate(np.arccos(1/np.sqrt(2))), [2, 3])
    qc.measure([0, 1, 2, 3], [0, 1, 2, 3])
    return qc
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
def qpe_grover00_gate(n_count=3) ->QuantumCircuit: 
    
    """
        Use Grover’s algorithm to mark state |00⟩, then apply a 3 qubits Quantum Phase Estimation using the Grover operator as the unitary.
        Return the QuantumCircuit after measure on first 3 qubits only.
    """

Framework: qiskit | Category: QUANTUM_ALGO

Solution:
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from qiskit.circuit.library import QFT

def qpe_grover00_gate(n_count=3):
    def grover_search_oracle_00() ->QuantumCircuit:
        qc = QuantumCircuit(2)
        qc.h(0)
        qc.h(1)
        qc.x(0)
        qc.x(1)
        qc.h(1)
        qc.cx(0,1)
        qc.x(0)
        qc.h(1)
        qc.h(0)
        qc.x(1)
        qc.h(1)
        return qc

    grover_00 = grover_search_oracle_00().to_gate()
    cgrover_00 = grover_00.control(1)
    qc = QuantumCircuit(n_count + 2, n_count)
    qc.h(range(n_count))
    qc.x(range(n_count,n_count+2))

    for qubit in range(n_count):
        repetitions = 2**qubit
        for _ in range(repetitions):
            qc.append(cgrover_00,[qubit, n_count,n_count+1])

    qc.append(QFT(n_count, inverse=True, do_swaps=True).to_gate(), range(n_count))

    qc.measure(range(n_count), range(n_count))

    return qc
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
def GHZ3_SWAPTEST() ->QuantumCircuit: 
    
    """
        Prepare a 3-qubit GHZ state and compare it with a |+++> state using the SWAP test. Use 3 ancilla qubits and measure ancilla qubits.
    """

Framework: qiskit | Category: QUANTUM_ALGO

Solution:
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
def GHZ3_SWAPTEST() ->QuantumCircuit: 
    qc = QuantumCircuit(9,3)
    qc.h([0,1,2])
    qc.h(3)
    qc.cx(3,4)
    qc.cx(4,5)
    qc.h([6,7,8])
    for i in range(3):
        qc.cswap(i,i+2,i+5)
    qc.h([0,1,2])
    qc.measure([0,1,2],[0,1,2])
    return qc
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
def decompose_CNOT() ->QuantumCircuit: 
    
    """
        Decompose CNOT gate use Hadmard gate and CZ gate. Return the QuantumCircuit without measure.
    """

Framework: qiskit | Category: DECOMPOSITION

Solution:
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
def decompose_CNOT() ->QuantumCircuit: 
    qc = QuantumCircuit(2,2)
    qc.h(1)
    qc.cz(0,1)
    qc.h(1)
    qc.measure(0,0)
    qc.measure(1,1)
    return qc
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
def decompose_CCCNOT() ->QuantumCircuit: 
    
    """
        Implement a 5 qubits quantum circuit, construct a decomposition of the CCCNOT (triple-controlled NOT) gate using CCNOT gate. qubit0, qubit1, and qubit2 act as control qubits, qubit 3 is ancilla qubit, and qubit4 is the target qubit.
        Return the QuantumCircuit after measure control and target qubits.
    """

Framework: qiskit | Category: QUANTUM_ALGO

Solution:
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
def decompose_CCCNOT() ->QuantumCircuit: 
    qc = QuantumCircuit(5, 4)
    qc.ccx(0, 1, 3)
    qc.ccx(2, 3, 4)
    qc.ccx(0, 1, 3)

    qc.measure([0,1,2,3], [0,1,2,3])
    return qc