Beispiel #1
0
def randBits(bits):
    number = 0
    for i in range(bits):
        bit = simulate(qc, shots=1, get='memory')
        if bit[0][0] == '1':
            number += 2**i
    return number
Beispiel #2
0
 def b_measure(self, qc):
     bob_base = random.choice("zx") #bob picks a random basis to measure his qubit
     self.bob_bases.append(bob_base)
     if bob_base == 'x': qc.h(0) #coverts to x basis: H|+> = |0>, H|+> = |1> (and vice versa)
     qc.measure(0,0)
     counts = simulate(qc, shots=1)
     result = '1' in counts
     if random.random() < self.e_m: result = not(result) #does the wrong measurement with probability e_m
     self.bob_bits.append(int(result)) #stores the result
Beispiel #3
0
def randGen():
    digits = []
    for _ in range(3):
        counts = simulate(qc, shots=1, get='memory')
        digits += counts

    BinString = ''.join(digits)

    BinNum = int(BinString, 2)

    return BinNum
Beispiel #4
0
def qRand(nMsrmnts):
    results = []

    for i in range(nMsrmnts):
        results += simulate(qc, 1, '')[0]

    binary = ''

    for i in range(len(results)):
        binary += results[i]

    decimal = int(binary, 2)
    return decimal
Beispiel #5
0
def circuit_to_height(qc):

    n = qc._n

    line = make_line(2**n)

    real_vec = simulate(qc, get='statevector')

    height = [0] * (2**n)
    for j, amp in enumerate(real_vec):
        string = "{0:b}".format(j)
        string = '0' * (n - len(string)) + string
        k = line.index(string)
        height[k] = amp[0]**2

    max_prob = max(height)
    for j, h in enumerate(height):
        height[j] = h / max_prob

    return height
Beispiel #6
0
def get_brightness(x, y, qc, seed):
    qc.data.clear()  # empty the circuit

    # perform rotations whose angles depend on x and y
    qc.rx((1 / 8) * (seed[0] * x - seed[1] * y) * pi, 0)
    qc.ry((1 / 8) * (seed[2] * x + seed[3] * y**2) * pi + pi, 0)

    # calculate probability for outcome 1
    qc.measure(0, 0)
    p = simulate(qc, shots=1000, get='counts')['1'] / 1000
    # return brightness depending on this probability
    # the chosen values here are fairly arbitrary
    if p > 0.7:
        if p < 0.8:
            return 1
        elif p < 0.9:
            return 2
        else:
            return 3
    else:
        return 0
Beispiel #7
0
def _circuit2probs(qc):
    """
    Runs the given circuit, and returns the resulting probabilities.
    """
    if simple_python:
        probs = simulate(qc, get='probabilities_dict')
    else:
        # separate circuit and initialization
        new_qc = qc.copy()
        new_qc.data = []
        initial_ket = [1]
        for gate in qc.data:
            if gate[0].name == 'initialize':
                initial_ket = _kron(initial_ket, gate[0].params)
            else:
                new_qc.data.append(gate)
        # then run it
        ket = quantum_info.Statevector(initial_ket)
        ket = ket.evolve(new_qc)
        probs = ket.probabilities_dict()

    return probs
Beispiel #8
0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import random
import pew
from microqiskit import QuantumCircuit, simulate

qc = QuantumCircuit(1, 1)
qc.h(0)
qc.measure(0, 0)

counts = simulate(qc, 1, '')


def qRand(nMsrmnts):
    results = []

    for i in range(nMsrmnts):
        results += simulate(qc, 1, '')[0]

    binary = ''

    for i in range(len(results)):
        binary += results[i]

    decimal = int(binary, 2)
    return decimal
Beispiel #9
0
for (X, Y) in [(1, 4), (6, 4)]:
    screen.pixel(X, Y, 0)  # turn off the center pixels of the squares

old_keys = 0
while True:  # loop which checks for user input and responds

    # look for and act upon key presses
    keys = pew.keys()  # get current key presses
    if keys != 0 and keys != old_keys:
        if keys & pew.K_O:
            qc.cx(1, 0)

    old_keys = keys

    # execute the circuit and get a single sample of memory for the given measurement bases
    m = simulate(qc + meas, shots=1, get='memory')

    # turn the pixels (1,2) and (1,4) (depending on basis[1]) on or off (depending on m[0][0])
    if m[0][0] == '1':

        screen.pixel(6, 4, 3)

    else:

        screen.pixel(6, 4, 0)

    # do the same for pixels (6,2) and (6,4)
    if m[0][1] == '1':

        screen.pixel(1, 4, 3)
Beispiel #10
0
                      int(instruct[i + 3]))
            if instruct[i] == "crx":
                qc.crx(float(instruct[i + 1]), int(instruct[i + 2]),
                       int(instruct[i + 3]))
            if instruct[i] == "m":
                try:
                    if instruct[i + 1] == ".":
                        for j in range(qn):
                            qc.measure(j, j)
                    else:
                        qc.measure(int(instruct[i + 1]), int(instruct[i + 1]))
                except Exception as e:
                    print(
                        "Instruction of measurement is Invalid. Please check the measurement:",
                        e)
        else:
            continue

    result = ""

    if prob:
        result = mq.simulate(qc, get="probabilities_dict")
    elif state:
        result = mq.simulate(qc, get="statevector")
    else:
        result = mq.simulate(qc)

    print(result)

except:
    print("Invalid instruction arguments. Please Check the arguments")